1
0

Client.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 "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_lPos > -1 && Info.m_lPos < MAX_SEND_CLIENTS)
  23. {
  24. Info.m_csIP = g_Opt.m_SendClients[Info.m_lPos].csIP;
  25. }
  26. else
  27. {
  28. Info.m_csErrorText = StrF(_T("ERROR getting ip position - %d"), Info.m_lPos);
  29. LogSendRecieveInfo(Info.m_csErrorText);
  30. return FALSE;
  31. }
  32. LogSendRecieveInfo(StrF(_T("Sending clip to %s"), Info.m_csIP));
  33. CClient client;
  34. if(client.OpenConnection(Info.m_csIP) == FALSE)
  35. {
  36. Info.m_csErrorText = StrF(_T("ERROR opening connection to %s"), Info.m_csIP);
  37. LogSendRecieveInfo(Info.m_csErrorText);
  38. return FALSE;
  39. }
  40. long lCount = Info.m_pClipList->GetCount();
  41. int i = -1;
  42. CClip* pClip;
  43. POSITION pos;
  44. pos = Info.m_pClipList->GetHeadPosition();
  45. while(pos)
  46. {
  47. pClip = Info.m_pClipList->GetNext(pos);
  48. if(pClip == NULL)
  49. {
  50. ASSERT(FALSE);
  51. continue;
  52. }
  53. i++;
  54. if(Info.m_pPopup)
  55. {
  56. Info.m_pPopup->SendToolTipText(StrF(_T("Sending %d of %d"), i+1, lCount));
  57. }
  58. MSG msg;
  59. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  60. {
  61. TranslateMessage(&msg);
  62. DispatchMessage(&msg);
  63. }
  64. LogSendRecieveInfo(StrF(_T("Sending %d of %d clip to %s"), i+1, lCount, Info.m_csIP));
  65. if(client.SendItem(pClip) == FALSE)
  66. {
  67. Info.m_csErrorText = "ERROR SendItem Failed";
  68. LogSendRecieveInfo(Info.m_csErrorText);
  69. return FALSE;
  70. }
  71. }
  72. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@");
  73. return TRUE;
  74. }
  75. UINT SendClientThread(LPVOID pParam)
  76. {
  77. EnterCriticalSection(&theApp.m_CriticalSection);
  78. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF SendClientThread - @@@@@@@@@@@@@@@");
  79. CClipList *pClipList = (CClipList*)pParam;
  80. if(pClipList == NULL)
  81. {
  82. LogSendRecieveInfo("ERROR if(pClipList == NULL)");
  83. return FALSE;
  84. }
  85. long lCount = pClipList->GetCount();
  86. LogSendRecieveInfo(StrF(_T("Start of Send ClientThread Count - %d"), lCount));
  87. for(int nClient = 0; nClient < MAX_SEND_CLIENTS; nClient++)
  88. {
  89. if(g_Opt.m_SendClients[nClient].bSendAll &&
  90. g_Opt.m_SendClients[nClient].csIP.GetLength() > 0)
  91. {
  92. CClient client;
  93. if(client.OpenConnection(g_Opt.m_SendClients[nClient].csIP) == FALSE)
  94. {
  95. LogSendRecieveInfo(StrF(_T("ERROR opening connection to %s"), g_Opt.m_SendClients[nClient].csIP));
  96. if(g_Opt.m_SendClients[nClient].bShownFirstError == FALSE)
  97. {
  98. CString cs;
  99. cs.Format(_T("Error opening connection to %s"), g_Opt.m_SendClients[nClient].csIP);
  100. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  101. cs.ReleaseBuffer();
  102. g_Opt.m_SendClients[nClient].bShownFirstError = TRUE;
  103. }
  104. continue;
  105. }
  106. //We were connected successfully show an error next time we can't connect
  107. g_Opt.m_SendClients[nClient].bShownFirstError = FALSE;
  108. CClip* pClip;
  109. POSITION pos;
  110. pos = pClipList->GetHeadPosition();
  111. while(pos)
  112. {
  113. pClip = pClipList->GetNext(pos);
  114. if(pClip == NULL)
  115. {
  116. ASSERT(FALSE);
  117. LogSendRecieveInfo("Error in GetNext");
  118. break;
  119. }
  120. LogSendRecieveInfo(StrF(_T("Sending clip to %s"), g_Opt.m_SendClients[nClient].csIP));
  121. if(client.SendItem(pClip) == FALSE)
  122. {
  123. CString cs;
  124. cs.Format(_T("Error sending clip to %s"), g_Opt.m_SendClients[nClient].csIP);
  125. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  126. cs.ReleaseBuffer();
  127. break;
  128. }
  129. }
  130. client.CloseConnection();
  131. }
  132. }
  133. delete pClipList;
  134. pClipList = NULL;
  135. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
  136. LeaveCriticalSection(&theApp.m_CriticalSection);
  137. return TRUE;
  138. }
  139. CClient::CClient()
  140. {
  141. m_Connection = NULL;
  142. }
  143. CClient::~CClient()
  144. {
  145. CloseConnection();
  146. }
  147. BOOL CClient::CloseConnection()
  148. {
  149. if(m_Connection != NULL && m_Connection != 0)
  150. {
  151. CSendInfo Info;
  152. m_SendSocket.SendCSendData(Info, MyEnums::EXIT);
  153. closesocket(m_Connection);
  154. WSACleanup();
  155. m_Connection = NULL;
  156. }
  157. return TRUE;
  158. }
  159. BOOL CClient::OpenConnection(const TCHAR* servername)
  160. {
  161. WSADATA wsaData;
  162. struct hostent *hp;
  163. unsigned int addr;
  164. struct sockaddr_in server;
  165. int wsaret=WSAStartup(0x101,&wsaData);
  166. if(wsaret)
  167. {
  168. LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)");
  169. return FALSE;
  170. }
  171. m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  172. if(m_Connection == INVALID_SOCKET)
  173. {
  174. LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
  175. m_Connection = NULL;
  176. return FALSE;
  177. }
  178. CStringA csServerNameA = CTextConvert::ConvertToChar(servername);
  179. if(inet_addr(csServerNameA)==INADDR_NONE)
  180. {
  181. hp = gethostbyname(csServerNameA);
  182. }
  183. else
  184. {
  185. addr = inet_addr(csServerNameA);
  186. hp = gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
  187. }
  188. if(hp == NULL)
  189. {
  190. LogSendRecieveInfo("ERROR - if(hp==NULL)");
  191. closesocket(m_Connection);
  192. m_Connection = NULL;
  193. return FALSE;
  194. }
  195. server.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
  196. server.sin_family = AF_INET;
  197. server.sin_port = htons((u_short) g_Opt.m_lPort);
  198. if(connect(m_Connection, (struct sockaddr*)&server, sizeof(server)))
  199. {
  200. int nWhy = WSAGetLastError();
  201. LogSendRecieveInfo(StrF(_T("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server))) why = %d"), nWhy));
  202. closesocket(m_Connection);
  203. m_Connection = NULL;
  204. return FALSE;
  205. }
  206. return TRUE;
  207. }
  208. BOOL CClient::SendItem(CClip *pClip)
  209. {
  210. CSendInfo Info;
  211. //Send all text over as UTF-8
  212. CStringA dest;
  213. if(CTextConvert::ConvertToUTF8(GetComputerName(), dest))
  214. {
  215. strncpy(Info.m_cComputerName, dest, sizeof(Info.m_cComputerName));
  216. }
  217. if(CTextConvert::ConvertToUTF8(GetIPAddress(), dest))
  218. {
  219. strncpy(Info.m_cIP, dest, sizeof(Info.m_cIP));
  220. }
  221. if(CTextConvert::ConvertToUTF8(pClip->m_Desc, dest))
  222. {
  223. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  224. }
  225. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  226. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  227. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  228. m_SendSocket.SetSocket(m_Connection);
  229. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  230. return FALSE;
  231. CClipFormat* pCF;
  232. int nCount = pClip->m_Formats.GetSize();
  233. //For each data type
  234. for( int i=0; i < nCount; i++ )
  235. {
  236. pCF = &pClip->m_Formats.GetData()[i];
  237. SendClipFormat(pCF);
  238. }
  239. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  240. return FALSE;
  241. theApp.m_lClipsSent++;
  242. return TRUE;
  243. }
  244. BOOL CClient::SendClipFormat(CClipFormat* pCF)
  245. {
  246. CSendInfo Info;
  247. LPVOID pvData = GlobalLock(pCF->m_hgData);
  248. long lLength = GlobalSize(pCF->m_hgData);
  249. UCHAR* pOutput = NULL;
  250. int nLenOutput = 0;
  251. CTextConvert Convert;
  252. BOOL bRet = FALSE;
  253. LogSendRecieveInfo(StrF(_T("BEFORE Encrypt clip data %d"), lLength));
  254. if(m_SendSocket.m_pEncryptor)
  255. {
  256. if(m_SendSocket.m_pEncryptor->Encrypt((UCHAR*)pvData, lLength, g_Opt.m_csPassword, pOutput, nLenOutput))
  257. {
  258. LogSendRecieveInfo(StrF(_T("AFTER Encrypt clip data %d"), nLenOutput));
  259. Info.m_lParameter1 = nLenOutput;
  260. //Send over as UTF-8
  261. CStringA dest;
  262. if(CTextConvert::ConvertToUTF8(GetFormatName(pCF->m_cfType), dest))
  263. {
  264. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  265. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  266. }
  267. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_START) == FALSE)
  268. return FALSE;
  269. m_SendSocket.SendExactSize((char*)pOutput, nLenOutput, false);
  270. m_SendSocket.m_pEncryptor->FreeBuffer(pOutput);
  271. bRet = TRUE;
  272. }
  273. else
  274. {
  275. LogSendRecieveInfo("Failed to encrypt data");
  276. return FALSE;
  277. }
  278. }
  279. else
  280. {
  281. ASSERT(!"SendItem::Encryption not initialized");
  282. LogSendRecieveInfo("SendItem::Encryption not initialized");
  283. }
  284. GlobalUnlock(pCF->m_hgData);
  285. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_END) == FALSE)
  286. return FALSE;
  287. return bRet;
  288. }
  289. HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CString csComputerName)
  290. {
  291. CSendInfo Info;
  292. bool bBreak = false;
  293. HGLOBAL hReturn = NULL;
  294. CString csErrorString;
  295. CFileTransferProgressDlg *pProgress = new CFileTransferProgressDlg;
  296. if(pProgress == NULL)
  297. return NULL;
  298. pProgress->Create(IDD_DIALOG_REMOTE_FILE);
  299. pProgress->ShowWindow(SW_SHOW);
  300. pProgress->SetMessage(StrF(_T("Opening Connection to %s (%s)"), csComputerName, csIP));
  301. pProgress->PumpMessages();
  302. do
  303. {
  304. if(OpenConnection(csIP) == FALSE)
  305. {
  306. csErrorString.Format(_T("Error Opening Connection to %s (%s)"), csComputerName, csIP);
  307. break;
  308. }
  309. m_SendSocket.SetSocket(m_Connection);
  310. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  311. break;
  312. if(SendClipFormat(&HDropFormat) == FALSE)
  313. {
  314. csErrorString = _T("Error sending data request.");
  315. break;
  316. }
  317. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  318. break;
  319. if(m_SendSocket.SendCSendData(Info, MyEnums::REQUEST_FILES) == FALSE)
  320. break;
  321. CFileRecieve Recieve;
  322. long lRet = Recieve.RecieveFiles(m_Connection, csIP, pProgress);
  323. if(lRet == TRUE)
  324. {
  325. hReturn = Recieve.CreateCF_HDROPBuffer();
  326. }
  327. else if(lRet == FALSE)
  328. {
  329. csErrorString = _T("Error recieving files.");
  330. }
  331. } while(false);
  332. CloseConnection();
  333. if(hReturn == NULL && csErrorString.IsEmpty() == FALSE)
  334. {
  335. MessageBox(pProgress->m_hWnd, csErrorString, _T("Ditto"), MB_OK|MB_ICONEXCLAMATION);
  336. }
  337. pProgress->DestroyWindow();
  338. return hReturn;
  339. }