Client.cpp 10.0 KB

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