Server.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. local.sin_addr.s_addr = INADDR_ANY;
  35. local.sin_port = htons((u_short)g_Opt.m_lPort);
  36. theApp.m_sSocket = socket(AF_INET, SOCK_STREAM, 0);
  37. if(theApp.m_sSocket == INVALID_SOCKET)
  38. {
  39. LogSendRecieveInfo("ERROR - theApp.m_sSocket = socket(AF_INET, SOCK_STREAM, 0);");
  40. return 0;
  41. }
  42. if(bind(theApp.m_sSocket,(sockaddr*)&local,sizeof(local))!=0)
  43. {
  44. LogSendRecieveInfo("ERROR - if(bind(theApp.m_sSocket,(sockaddr*)&local,sizeof(local))!=0)");
  45. return 0;
  46. }
  47. if(listen(theApp.m_sSocket,10)!=0)
  48. {
  49. LogSendRecieveInfo("ERROR - if(listen(theApp.m_sSocket,10)!=0)");
  50. return 0;
  51. }
  52. SOCKET socket;
  53. sockaddr_in from;
  54. int fromlen = sizeof(from);
  55. while(true)
  56. {
  57. if(theApp.m_bAppExiting || theApp.m_bExitServerThread)
  58. break;
  59. socket = accept(theApp.m_sSocket, (struct sockaddr*)&from,&fromlen);
  60. if( socket != INVALID_SOCKET )
  61. AfxBeginThread(ClientThread,(LPVOID)socket);
  62. }
  63. LogSendRecieveInfo("End of Server Thread");
  64. bRunning = false;
  65. theApp.m_sSocket = INVALID_SOCKET;
  66. return 0;
  67. }
  68. UINT ClientThread(LPVOID pParam)
  69. {
  70. LogSendRecieveInfo("*********************Start of ClientThread*********************");
  71. CServer Server;
  72. Server.RunThread((SOCKET)pParam);
  73. LogSendRecieveInfo("*********************End of ClientThread*********************");
  74. return 0;
  75. }
  76. CServer::CServer()
  77. {
  78. m_pClipList = NULL;
  79. m_pClip = NULL;
  80. m_bSetToClipBoard = FALSE;
  81. }
  82. CServer::~CServer()
  83. {
  84. closesocket(m_Sock.GetSocket());
  85. }
  86. void CServer::RunThread(SOCKET sock)
  87. {
  88. m_Sock.SetSocket(sock);
  89. CSendInfo info;
  90. bool bBreak = false;
  91. while(true)
  92. {
  93. if(m_Sock.RecieveCSendInfo(&info) == FALSE)
  94. break;
  95. switch(info.m_Type)
  96. {
  97. case MyEnums::START:
  98. OnStart(info);
  99. break;
  100. case MyEnums::DATA_START:
  101. OnDataStart(info);
  102. break;
  103. case MyEnums::DATA_END:
  104. OnDataEnd(info);
  105. break;
  106. case MyEnums::END:
  107. OnEnd(info);
  108. break;
  109. case MyEnums::EXIT:
  110. OnExit(info);
  111. bBreak = true;
  112. break;
  113. case MyEnums::REQUEST_FILES:
  114. OnRequestFiles(info);
  115. break;
  116. default:
  117. LogSendRecieveInfo("::ERROR unknown action type exiting");
  118. bBreak = true;
  119. }
  120. if(bBreak || theApp.m_bAppExiting)
  121. break;
  122. }
  123. if(m_pClipList)
  124. {
  125. LogSendRecieveInfo("::ERROR pClipList was not NULL something is wrong");
  126. delete m_pClipList;
  127. m_pClipList = NULL;
  128. }
  129. if(m_pClip)
  130. {
  131. LogSendRecieveInfo("::ERROR pClip was not NULL something is wrong");
  132. delete m_pClip;
  133. m_pClip = NULL;
  134. }
  135. }
  136. void CServer::OnStart(CSendInfo &info)
  137. {
  138. CTextConvert::ConvertFromUTF8(info.m_cIP, m_csIP);
  139. CTextConvert::ConvertFromUTF8(info.m_cComputerName, m_csComputerName);
  140. CTextConvert::ConvertFromUTF8(info.m_cDesc, m_csDesc);
  141. if(m_pClip != NULL)
  142. {
  143. delete m_pClip;
  144. m_pClip = NULL;
  145. }
  146. m_pClip = new CClip;
  147. CString cs;
  148. cs.Format(_T("%s\n(%s)(%s)"), m_csDesc, m_csComputerName, m_csIP);
  149. if(m_pClip)
  150. {
  151. m_pClip->m_Desc = cs;
  152. }
  153. m_bSetToClipBoard = FALSE;
  154. CTokenizer token(g_Opt.m_csIPListToPutOnClipboard, ",");
  155. CString line;
  156. while(token.Next(line))
  157. {
  158. if(line != "")
  159. {
  160. if(CWildCardMatch::WildMatch(line, m_csIP, ""))
  161. {
  162. LogSendRecieveInfo(StrF(_T("Found ip match, placing on clipboard Found Match %s - %s"), line, m_csIP));
  163. m_bSetToClipBoard = TRUE;
  164. break;
  165. }
  166. if(CWildCardMatch::WildMatch(line, m_csComputerName, ""))
  167. {
  168. LogSendRecieveInfo(StrF(_T("Found machine match, placing on clipboard Found Match %s - %s"), line, m_csIP));
  169. m_bSetToClipBoard = TRUE;
  170. break;
  171. }
  172. }
  173. }
  174. info.m_cDesc[20] = 0;
  175. LogSendRecieveInfo(StrF(_T("::START %s %s %s"), m_csDesc, m_csComputerName, m_csIP));
  176. }
  177. void CServer::OnDataStart(CSendInfo &info)
  178. {
  179. LogSendRecieveInfo("::DATA_START -- START");
  180. CString csFormat;
  181. CTextConvert::ConvertFromUTF8(info.m_cDesc, csFormat);
  182. m_cf.m_cfType = GetFormatID(csFormat);
  183. m_cf.m_hgData = 0;
  184. long lInSize = info.m_lParameter1;
  185. long lOutSize = 0;
  186. LPVOID lpData = m_Sock.ReceiveEncryptedData(lInSize, lOutSize);
  187. if(lpData && lOutSize > 0)
  188. {
  189. m_cf.m_hgData = NewGlobal(lOutSize);
  190. if(m_cf.m_hgData)
  191. {
  192. if(m_pClip)
  193. {
  194. m_pClip->m_lTotalCopySize += lOutSize;
  195. }
  196. LPVOID pvData = GlobalLock(m_cf.m_hgData);
  197. if(pvData)
  198. {
  199. memcpy(pvData, lpData, lOutSize);
  200. GlobalUnlock(m_cf.m_hgData);
  201. }
  202. else
  203. {
  204. LogSendRecieveInfo("::DATA_START -- failed to lock hGlobal");
  205. }
  206. }
  207. else
  208. {
  209. LogSendRecieveInfo("::DATA_START -- failed to create new hGlobal");
  210. }
  211. m_Sock.FreeDecryptedData();
  212. }
  213. LogSendRecieveInfo("::DATA_START -- END");
  214. }
  215. void CServer::OnDataEnd(CSendInfo &info)
  216. {
  217. LogSendRecieveInfo("::DATA_END");
  218. if(m_pClip && m_cf.m_hgData)
  219. {
  220. if(m_cf.m_cfType == CF_HDROP)
  221. AddRemoteCF_HDROPFormat();
  222. m_pClip->m_Formats.Add(m_cf);
  223. m_cf.m_hgData = 0; // now owned by pClip
  224. }
  225. else
  226. {
  227. LogSendRecieveInfo("MyEnums::DATA_END Error if(pClip && cf.m_hgData)");
  228. }
  229. }
  230. void CServer::OnEnd(CSendInfo &info)
  231. {
  232. LogSendRecieveInfo("::END");
  233. if(m_pClipList == NULL)
  234. m_pClipList = new CClipList;
  235. if(m_pClipList)
  236. {
  237. m_pClipList->AddTail(m_pClip);
  238. m_pClip = NULL; //clip list now owns the clip
  239. }
  240. else
  241. LogSendRecieveInfo("::ERROR pClipList was NULL");
  242. }
  243. void CServer::OnExit(CSendInfo &info)
  244. {
  245. LogSendRecieveInfo("::EXIT");
  246. if(m_pClipList && m_pClipList->GetCount() > 0)
  247. {
  248. theApp.m_lClipsRecieved += (long)m_pClipList->GetCount();
  249. //Post a message pClipList will be freed by the reciever
  250. ::PostMessage(theApp.m_MainhWnd, WM_ADD_TO_DATABASE_FROM_SOCKET, (WPARAM)m_pClipList, m_bSetToClipBoard);
  251. m_pClipList = NULL;
  252. }
  253. else
  254. LogSendRecieveInfo("::ERROR pClipList was NULL or Count was 0");
  255. }
  256. void CServer::OnRequestFiles(CSendInfo &info)
  257. {
  258. CFileSend Send;
  259. Send.SendClientFiles(m_Sock.GetSocket(), m_pClipList);
  260. delete m_pClipList;
  261. m_pClipList = NULL;
  262. }
  263. void CServer::AddRemoteCF_HDROPFormat()
  264. {
  265. CClipFormat cf;
  266. CDittoCF_HDROP Drop;
  267. CTextConvert Convert;
  268. CStringA dest;
  269. if(CTextConvert::ConvertToUTF8(m_csIP, dest))
  270. {
  271. strncpy(Drop.m_cIP, dest, sizeof(Drop.m_cIP)-1);
  272. }
  273. if(CTextConvert::ConvertToUTF8(m_csComputerName, dest))
  274. {
  275. strncpy(Drop.m_cComputerName, dest, sizeof(Drop.m_cComputerName)-1);
  276. }
  277. cf.m_hgData = NewGlobalP(&Drop, sizeof(Drop));
  278. cf.m_cfType = theApp.m_RemoteCF_HDROP;
  279. m_pClip->m_Formats.Add(cf);
  280. //m_pClip->m_Formats now owns the data
  281. cf.m_hgData = NULL;
  282. }