Client.cpp 8.0 KB

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