Client.cpp 8.5 KB

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