Server.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Server.cpp: implementation of the CServer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "Server.h"
  7. #include "Shared\Tokenizer.h"
  8. #include "WildCardMatch.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. UINT MTServerThread(LPVOID pParam)
  18. {
  19. static bool bRunning = false;
  20. if(bRunning)
  21. return 0;
  22. bRunning = true;
  23. LogSendRecieveInfo("Start of ServerThread");
  24. theApp.m_bExitServerThread = false;
  25. WSADATA wsaData;
  26. sockaddr_in local;
  27. int wsaret = WSAStartup(0x101,&wsaData);
  28. if(wsaret!=0)
  29. {
  30. LogSendRecieveInfo("ERROR - int wsaret = WSAStartup(0x101,&wsaData);");
  31. return 0;
  32. }
  33. local.sin_family = AF_INET;
  34. CString bindToIpAddress = CGetSetOptions::GetNetworkBindIPAddress();
  35. if (bindToIpAddress == _T("*"))
  36. {
  37. local.sin_addr.s_addr = INADDR_ANY;
  38. }
  39. else
  40. {
  41. local.sin_addr.s_addr = inet_addr(CTextConvert::ConvertToChar(bindToIpAddress));
  42. }
  43. local.sin_port = htons((u_short)g_Opt.m_lPort);
  44. theApp.m_sSocket = socket(AF_INET, SOCK_STREAM, 0);
  45. if(theApp.m_sSocket == INVALID_SOCKET)
  46. {
  47. LogSendRecieveInfo("ERROR - theApp.m_sSocket = socket(AF_INET, SOCK_STREAM, 0);");
  48. return 0;
  49. }
  50. if(bind(theApp.m_sSocket,(sockaddr*)&local,sizeof(local))!=0)
  51. {
  52. LogSendRecieveInfo("ERROR - if(bind(theApp.m_sSocket,(sockaddr*)&local,sizeof(local))!=0)");
  53. return 0;
  54. }
  55. if(listen(theApp.m_sSocket,10)!=0)
  56. {
  57. LogSendRecieveInfo("ERROR - if(listen(theApp.m_sSocket,10)!=0)");
  58. return 0;
  59. }
  60. sockaddr_in from;
  61. int fromlen = sizeof(from);
  62. while(true)
  63. {
  64. if(theApp.m_bAppExiting || theApp.m_bExitServerThread)
  65. break;
  66. SOCKET socket = accept(theApp.m_sSocket, (struct sockaddr*)&from, &fromlen);
  67. SocketParams *pParams = new SocketParams();
  68. pParams->m_ip = inet_ntoa(from.sin_addr);
  69. pParams->m_socket = socket;
  70. if (socket != INVALID_SOCKET)
  71. {
  72. AfxBeginThread(ClientThread, (LPVOID)pParams);
  73. }
  74. else
  75. {
  76. delete pParams;
  77. }
  78. }
  79. LogSendRecieveInfo("End of Server Thread");
  80. bRunning = false;
  81. theApp.m_sSocket = INVALID_SOCKET;
  82. return 0;
  83. }
  84. UINT ClientThread(LPVOID pParam)
  85. {
  86. LogSendRecieveInfo("*********************Start of ClientThread*********************");
  87. SocketParams *pParams = (SocketParams*)pParam;
  88. CServer Server;
  89. Server.RunThread(pParams);
  90. delete pParams;
  91. LogSendRecieveInfo("*********************End of ClientThread*********************");
  92. return 0;
  93. }
  94. CServer::CServer()
  95. {
  96. m_pClipList = NULL;
  97. m_pClip = NULL;
  98. m_bSetToClipBoard = FALSE;
  99. m_manualSend = false;
  100. m_respondPort = 0;
  101. }
  102. CServer::~CServer()
  103. {
  104. closesocket(m_Sock.GetSocket());
  105. }
  106. void CServer::RunThread(SocketParams *pParams)
  107. {
  108. m_Sock.SetSocket(pParams->m_socket);
  109. m_recieveIP = pParams->m_ip;
  110. CSendInfo info;
  111. bool bBreak = false;
  112. while(true)
  113. {
  114. if(m_Sock.RecieveCSendInfo(&info) == FALSE)
  115. break;
  116. switch(info.m_Type)
  117. {
  118. case MyEnums::START:
  119. OnStart(info);
  120. break;
  121. case MyEnums::DATA_START:
  122. OnDataStart(info);
  123. break;
  124. case MyEnums::DATA_END:
  125. OnDataEnd(info);
  126. break;
  127. case MyEnums::END:
  128. OnEnd(info);
  129. break;
  130. case MyEnums::EXIT:
  131. OnExit(info);
  132. bBreak = true;
  133. break;
  134. case MyEnums::REQUEST_FILES:
  135. OnRequestFiles(info);
  136. break;
  137. default:
  138. LogSendRecieveInfo("::ERROR unknown action type exiting");
  139. bBreak = true;
  140. }
  141. if(bBreak || theApp.m_bAppExiting)
  142. break;
  143. }
  144. if(m_pClipList)
  145. {
  146. LogSendRecieveInfo("::ERROR pClipList was not NULL something is wrong");
  147. delete m_pClipList;
  148. m_pClipList = NULL;
  149. }
  150. if(m_pClip)
  151. {
  152. LogSendRecieveInfo("::ERROR pClip was not NULL something is wrong");
  153. delete m_pClip;
  154. m_pClip = NULL;
  155. }
  156. }
  157. void CServer::OnStart(CSendInfo &info)
  158. {
  159. if (m_recieveIP != _T("") &&
  160. g_Opt.GetUseIPFromAccept())
  161. {
  162. LogSendRecieveInfo(StrF(_T("Using ip address from the Accept Call - %s"), m_recieveIP));
  163. m_csIP = m_recieveIP;
  164. }
  165. else
  166. {
  167. CTextConvert::ConvertFromUTF8(info.m_cIP, m_csIP);
  168. }
  169. CTextConvert::ConvertFromUTF8(info.m_cComputerName, m_csComputerName);
  170. CTextConvert::ConvertFromUTF8(info.m_cDesc, m_csDesc);
  171. m_manualSend = info.m_manualSend;
  172. m_respondPort = info.m_respondPort;
  173. if(m_pClip != NULL)
  174. {
  175. delete m_pClip;
  176. m_pClip = NULL;
  177. }
  178. m_pClip = new CClip;
  179. CString cs;
  180. cs.Format(_T("%s\n(%s)(%s)"), m_csDesc, m_csComputerName, m_csIP);
  181. if(m_pClip)
  182. {
  183. m_pClip->m_Desc = cs;
  184. }
  185. m_bSetToClipBoard = FALSE;
  186. CTokenizer token(g_Opt.m_csIPListToPutOnClipboard, ",");
  187. CString line;
  188. while(token.Next(line))
  189. {
  190. if(line != "")
  191. {
  192. if(CWildCardMatch::WildMatch(line, m_csIP, ""))
  193. {
  194. LogSendRecieveInfo(StrF(_T("Found ip match, placing on clipboard Found Match %s - %s"), line, m_csIP));
  195. m_bSetToClipBoard = TRUE;
  196. break;
  197. }
  198. if(CWildCardMatch::WildMatch(line, m_csComputerName, ""))
  199. {
  200. LogSendRecieveInfo(StrF(_T("Found machine match, placing on clipboard Found Match %s - %s"), line, m_csIP));
  201. m_bSetToClipBoard = TRUE;
  202. break;
  203. }
  204. }
  205. }
  206. info.m_cDesc[20] = 0;
  207. LogSendRecieveInfo(StrF(_T("::START %s %s %s"), m_csDesc, m_csComputerName, m_csIP));
  208. }
  209. void CServer::OnDataStart(CSendInfo &info)
  210. {
  211. LogSendRecieveInfo("::DATA_START -- START");
  212. CString csFormat;
  213. CTextConvert::ConvertFromUTF8(info.m_cDesc, csFormat);
  214. m_cf.m_cfType = GetFormatID(csFormat);
  215. m_cf.m_hgData = 0;
  216. long lInSize = info.m_lParameter1;
  217. long lOutSize = 0;
  218. LPVOID lpData = m_Sock.ReceiveEncryptedData(lInSize, lOutSize);
  219. if(lpData && lOutSize > 0)
  220. {
  221. m_cf.m_hgData = NewGlobal(lOutSize);
  222. if(m_cf.m_hgData)
  223. {
  224. if(m_pClip)
  225. {
  226. m_pClip->m_lTotalCopySize += lOutSize;
  227. }
  228. LPVOID pvData = GlobalLock(m_cf.m_hgData);
  229. if(pvData)
  230. {
  231. memcpy(pvData, lpData, lOutSize);
  232. GlobalUnlock(m_cf.m_hgData);
  233. }
  234. else
  235. {
  236. LogSendRecieveInfo("::DATA_START -- failed to lock hGlobal");
  237. }
  238. }
  239. else
  240. {
  241. LogSendRecieveInfo("::DATA_START -- failed to create new hGlobal");
  242. }
  243. m_Sock.FreeDecryptedData();
  244. }
  245. LogSendRecieveInfo("::DATA_START -- END");
  246. }
  247. void CServer::OnDataEnd(CSendInfo &info)
  248. {
  249. LogSendRecieveInfo("::DATA_END");
  250. if(m_pClip && m_cf.m_hgData)
  251. {
  252. if(m_cf.m_cfType == CF_HDROP)
  253. AddRemoteCF_HDROPFormat();
  254. m_pClip->m_Formats.Add(m_cf);
  255. m_cf.m_hgData = 0; // now owned by pClip
  256. }
  257. else
  258. {
  259. LogSendRecieveInfo("MyEnums::DATA_END Error if(pClip && cf.m_hgData)");
  260. }
  261. }
  262. void CServer::OnEnd(CSendInfo &info)
  263. {
  264. LogSendRecieveInfo("::END");
  265. if(m_pClipList == NULL)
  266. m_pClipList = new CClipList;
  267. if(m_pClipList)
  268. {
  269. m_pClipList->AddTail(m_pClip);
  270. m_pClip = NULL; //clip list now owns the clip
  271. }
  272. else
  273. LogSendRecieveInfo("::ERROR pClipList was NULL");
  274. }
  275. void CServer::OnExit(CSendInfo &info)
  276. {
  277. LogSendRecieveInfo("::EXIT");
  278. if(m_pClipList && m_pClipList->GetCount() > 0)
  279. {
  280. theApp.m_lClipsRecieved += (long)m_pClipList->GetCount();
  281. DWORD flags = 0;
  282. if (m_bSetToClipBoard)
  283. {
  284. flags |= REMOTE_CLIP_ADD_TO_CLIPBOARD;
  285. }
  286. if (m_manualSend)
  287. {
  288. flags |= REMOTE_CLIP_MANUAL_SEND;
  289. }
  290. //Post a message pClipList will be freed by the reciever
  291. ::PostMessage(theApp.m_MainhWnd, WM_ADD_TO_DATABASE_FROM_SOCKET, (WPARAM)m_pClipList, flags);
  292. m_pClipList = NULL;
  293. }
  294. else
  295. LogSendRecieveInfo("::ERROR pClipList was NULL or Count was 0");
  296. }
  297. void CServer::OnRequestFiles(CSendInfo &info)
  298. {
  299. CFileSend Send;
  300. Send.SendClientFiles(m_Sock.GetSocket(), m_pClipList);
  301. delete m_pClipList;
  302. m_pClipList = NULL;
  303. }
  304. void CServer::AddRemoteCF_HDROPFormat()
  305. {
  306. CClipFormat cf;
  307. CDittoCF_HDROP Drop;
  308. Drop.respondPort = m_respondPort;
  309. CTextConvert Convert;
  310. CStringA dest;
  311. if(CTextConvert::ConvertToUTF8(m_csIP, dest))
  312. {
  313. strncpy(Drop.m_cIP, dest, sizeof(Drop.m_cIP)-1);
  314. }
  315. if(CTextConvert::ConvertToUTF8(m_csComputerName, dest))
  316. {
  317. strncpy(Drop.m_cComputerName, dest, sizeof(Drop.m_cComputerName)-1);
  318. }
  319. cf.m_hgData = NewGlobalP(&Drop, sizeof(Drop));
  320. cf.m_cfType = theApp.m_RemoteCF_HDROP;
  321. m_pClip->m_Formats.Add(cf);
  322. //m_pClip->m_Formats now owns the data
  323. cf.m_hgData = NULL;
  324. }