Client.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 = g_Opt.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::ConvertToChar(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 (g_Opt.m_lPort != 23443 || m_connectionPort != 23443)
  168. {
  169. Info.m_respondPort = g_Opt.m_lPort;
  170. }
  171. //Send all text over as UTF-8
  172. CStringA dest;
  173. if(CTextConvert::ConvertToUTF8(GetComputerName(), dest))
  174. {
  175. strncpy(Info.m_cComputerName, dest, sizeof(Info.m_cComputerName));
  176. }
  177. if(CTextConvert::ConvertToUTF8(GetIPAddress(), dest))
  178. {
  179. strncpy(Info.m_cIP, dest, sizeof(Info.m_cIP));
  180. }
  181. if(CTextConvert::ConvertToUTF8(pClip->m_Desc, dest))
  182. {
  183. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  184. }
  185. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  186. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  187. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  188. m_SendSocket.SetSocket(m_Connection);
  189. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  190. return FALSE;
  191. CClipFormat* pCF;
  192. INT_PTR count = pClip->m_Formats.GetSize();
  193. //For each data type
  194. for(int i=0; i < count; i++)
  195. {
  196. pCF = &pClip->m_Formats.GetData()[i];
  197. SendClipFormat(pCF);
  198. }
  199. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  200. return FALSE;
  201. theApp.m_lClipsSent++;
  202. return TRUE;
  203. }
  204. BOOL CClient::SendClipFormat(CClipFormat* pCF)
  205. {
  206. CSendInfo Info;
  207. LPVOID pvData = GlobalLock(pCF->m_hgData);
  208. INT_PTR length = GlobalSize(pCF->m_hgData);
  209. UCHAR* pOutput = NULL;
  210. int nLenOutput = 0;
  211. CTextConvert Convert;
  212. BOOL bRet = FALSE;
  213. LogSendRecieveInfo(StrF(_T("BEFORE Encrypt clip data %d"), length));
  214. if(m_SendSocket.m_pEncryptor)
  215. {
  216. if(m_SendSocket.m_pEncryptor->Encrypt((UCHAR*)pvData, (int)length, g_Opt.m_csPassword, pOutput, nLenOutput))
  217. {
  218. LogSendRecieveInfo(StrF(_T("AFTER Encrypt clip data %d"), nLenOutput));
  219. Info.m_lParameter1 = nLenOutput;
  220. //Send over as UTF-8
  221. CStringA dest;
  222. if(CTextConvert::ConvertToUTF8(GetFormatName(pCF->m_cfType), dest))
  223. {
  224. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  225. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  226. }
  227. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_START) == FALSE)
  228. return FALSE;
  229. m_SendSocket.SendExactSize((char*)pOutput, nLenOutput, false);
  230. m_SendSocket.m_pEncryptor->FreeBuffer(pOutput);
  231. bRet = TRUE;
  232. }
  233. else
  234. {
  235. LogSendRecieveInfo("Failed to encrypt data");
  236. return FALSE;
  237. }
  238. }
  239. else
  240. {
  241. ASSERT(!"SendItem::Encryption not initialized");
  242. LogSendRecieveInfo("SendItem::Encryption not initialized");
  243. }
  244. GlobalUnlock(pCF->m_hgData);
  245. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_END) == FALSE)
  246. return FALSE;
  247. return bRet;
  248. }
  249. HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CString csComputerName)
  250. {
  251. CSendInfo Info;
  252. bool bBreak = false;
  253. HGLOBAL hReturn = NULL;
  254. CString csErrorString;
  255. CFileTransferProgressDlg *pProgress = new CFileTransferProgressDlg;
  256. if(pProgress == NULL)
  257. return NULL;
  258. LogSendRecieveInfo(StrF(_T("************** START of requesting files from cpu %s, ip: %s **************"), csComputerName, csIP));
  259. pProgress->Create(IDD_DIALOG_REMOTE_FILE);
  260. pProgress->ShowWindow(SW_SHOW);
  261. pProgress->SetMessage(StrF(_T("Opening Connection to %s (%s)"), csComputerName, csIP));
  262. pProgress->PumpMessages();
  263. CString requestFrom;
  264. if(g_Opt.GetRequestFilesUsingIP())
  265. {
  266. requestFrom = csIP;
  267. }
  268. else
  269. {
  270. requestFrom = csComputerName;
  271. }
  272. do
  273. {
  274. if(OpenConnection(requestFrom) == FALSE)
  275. {
  276. csErrorString.Format(_T("Error Opening Connection to %s (%s)"), csComputerName, csIP);
  277. break;
  278. }
  279. m_SendSocket.SetSocket(m_Connection);
  280. m_SendSocket.SetProgressBar(pProgress);
  281. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  282. break;
  283. if(SendClipFormat(&HDropFormat) == FALSE)
  284. {
  285. csErrorString = _T("Error sending data request.");
  286. break;
  287. }
  288. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  289. break;
  290. pProgress->SetMessage(StrF(_T("Requesting Files from %s (%s)"), csComputerName, csIP));
  291. if(m_SendSocket.SendCSendData(Info, MyEnums::REQUEST_FILES) == FALSE)
  292. break;
  293. CFileRecieve Recieve;
  294. long lRet = Recieve.RecieveFiles(m_Connection, csIP, pProgress);
  295. if(lRet == TRUE)
  296. {
  297. hReturn = Recieve.CreateCF_HDROPBuffer();
  298. }
  299. else if(lRet == FALSE || lRet == MD5_MISMATCH)
  300. {
  301. if(pProgress != NULL && pProgress->Cancelled())
  302. {
  303. //Don't show an error message the user canceled things
  304. }
  305. else
  306. {
  307. csErrorString = _T("Error receiving files.");
  308. if (lRet == MD5_MISMATCH)
  309. {
  310. csErrorString += _T(" MD5 Match Error.");
  311. }
  312. }
  313. }
  314. } while(false);
  315. CloseConnection();
  316. if(hReturn == NULL && csErrorString.IsEmpty() == FALSE)
  317. {
  318. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)csErrorString.GetBuffer(csErrorString.GetLength()), 0);
  319. }
  320. pProgress->DestroyWindow();
  321. LogSendRecieveInfo(StrF(_T("************** END of requesting files from cpu %s, ip: %s **************************"), csComputerName, csIP));
  322. return hReturn;
  323. }