LYFZReceiveMsg.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /****************************************************************/
  2. /* */
  3. /* LYFZReceiveMsg.cpp */
  4. /* */
  5. /* Implementation of the CLYFZReceiveMsg class. */
  6. /* This class is a part of the FTP Server Application */
  7. /* */
  8. /* Programmed by LYFZ van der Meer */
  9. /* Copyright LYFZ Software Solutions 2002 */
  10. /* http://www.LYFZvandermeer.nl */
  11. /* */
  12. /* Last updated: 10 july 2002 */
  13. /* */
  14. /****************************************************************/
  15. #include "stdafx.h"
  16. #include "LYFZReceiveMsg.h"
  17. #include "MyLock.h"
  18. extern int g_port;
  19. CLYFZReceiveMsg *g_pWndServer=NULL;
  20. CLYFZReceiveMsg::CLYFZReceiveMsg()
  21. {
  22. m_nPort = g_port;
  23. m_nMaxUsers = 50;
  24. m_strWelcomeMessage = "Welcome to LYFZ FTP Server";
  25. m_strGoodbyeMessage = "Bye";
  26. m_nTimeout = 1;
  27. m_bRunning = FALSE;
  28. m_hWnd = NULL;
  29. m_nConnectionCount = 0;
  30. // intialize statistics
  31. m_dwTotalSentBytes = 0;
  32. m_dwTotalReceivedBytes = 0;
  33. m_nTotalConnections = 0;
  34. m_nFilesDownloaded = 0;
  35. m_nFilesUploaded = 0;
  36. m_nFailedDownloads = 0;
  37. m_nFailedUploads = 0;
  38. m_nSecurityMode = 0;
  39. m_nStatisticsInterval = 0;
  40. // load users
  41. m_UserManager.Serialize(FALSE);
  42. // load security
  43. m_SecurityManager.Serialize(FALSE);
  44. }
  45. CLYFZReceiveMsg::~CLYFZReceiveMsg()
  46. {
  47. Stop();
  48. }
  49. BEGIN_MESSAGE_MAP(CLYFZReceiveMsg, CWnd)
  50. //{{AFX_MSG_MAP(CLYFZReceiveMsg)
  51. ON_WM_TIMER()
  52. //}}AFX_MSG_MAP
  53. ON_MESSAGE(WM_THREADSTART, OnThreadStart)
  54. ON_MESSAGE(WM_THREADCLOSE, OnThreadClose)
  55. ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
  56. END_MESSAGE_MAP()
  57. /********************************************************************/
  58. /* */
  59. /* Function name : Start */
  60. /* Description : Start listining on port 21 and accept new */
  61. /* connections. */
  62. /* */
  63. /********************************************************************/
  64. //在21号端口启动侦听并准备接受新的连接
  65. BOOL CLYFZReceiveMsg::Start()
  66. {
  67. if (m_bRunning)
  68. return FALSE;
  69. // 为消息发送创建一个新的窗口
  70. if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), "FTP Server Notification Sink", WS_POPUP, 0,0,0,0, NULL, 0))
  71. {
  72. AddTraceLine(0, "Failed to create notification window.");
  73. return FALSE;
  74. }
  75. // 创建一个新的套接字
  76. if (m_ListenSocket.Create(g_port))
  77. {
  78. // 启动侦听
  79. if (m_ListenSocket.Listen())
  80. {
  81. g_pWndServer=this;
  82. // m_ListenSocket.m_pWndServer = this;
  83. m_bRunning = TRUE;
  84. SetTimer(1, m_nStatisticsInterval, NULL);
  85. AddTraceLine(0, "FTP Server started on port %d.", g_port);
  86. return TRUE;
  87. }
  88. }
  89. AddTraceLine(0, "FTP Server failed to listen on port %d.", g_port);
  90. // 销毁通知窗口
  91. if (IsWindow(m_hWnd))
  92. DestroyWindow();
  93. m_hWnd = NULL;
  94. return FALSE;
  95. }
  96. /********************************************************************/
  97. /* */
  98. /* Function name : Stop */
  99. /* Description : Stop FTP server. */
  100. /* */
  101. /********************************************************************/
  102. //停止FTP服务器
  103. void CLYFZReceiveMsg::Stop()
  104. {
  105. if (!m_bRunning)
  106. return;
  107. // 停止统计计时器
  108. KillTimer(1);
  109. m_bRunning = FALSE;
  110. m_ListenSocket.Close();
  111. CConnectThread* pThread = NULL;
  112. //关闭所有线程
  113. do
  114. {
  115. m_CriticalSection.Lock();
  116. POSITION pos = m_ThreadList.GetHeadPosition();
  117. if (pos != NULL)
  118. {
  119. pThread = (CConnectThread *)m_ThreadList.GetAt(pos);
  120. m_CriticalSection.Unlock();
  121. // 保存线程
  122. int nThreadID = pThread->m_nThreadID;
  123. HANDLE hThread = pThread->m_hThread;
  124. AddTraceLine(0, "[%d] Shutting down thread...", nThreadID);
  125. // 通知线程停止
  126. pThread->SetThreadPriority(THREAD_PRIORITY_HIGHEST);
  127. pThread->PostThreadMessage(WM_QUIT,0,0);
  128. //等待线程终止
  129. if (WaitWithMessageLoop(hThread, 5000) == FALSE)
  130. {
  131. // 线程不能终止
  132. AddTraceLine(0, "[%d] Problem while killing thread.", nThreadID);
  133. //清除线程
  134. m_CriticalSection.Lock();
  135. POSITION rmPos = m_ThreadList.Find(pThread);
  136. if (rmPos != NULL)
  137. m_ThreadList.RemoveAt(rmPos);
  138. m_CriticalSection.Unlock();
  139. }
  140. else
  141. {
  142. AddTraceLine(0, "[%d] Thread successfully stopped.", nThreadID);
  143. }
  144. }
  145. else
  146. {
  147. m_CriticalSection.Unlock();
  148. pThread = NULL;
  149. }
  150. }
  151. while (pThread != NULL);
  152. //更新服务器状态
  153. AddTraceLine(0, "FTP Server stopped.");
  154. if (IsWindow(m_hWnd))
  155. DestroyWindow();
  156. m_hWnd = NULL;
  157. }
  158. /********************************************************************/
  159. /* */
  160. /* Function name : IsActive */
  161. /* Description : Is FTP server active? */
  162. /* */
  163. /********************************************************************/
  164. BOOL CLYFZReceiveMsg::IsActive()
  165. {
  166. return m_bRunning;
  167. }
  168. /********************************************************************/
  169. /* */
  170. /* Function name : SetMaxUsers */
  171. /* Description : Set maximum number of users */
  172. /* */
  173. /********************************************************************/
  174. void CLYFZReceiveMsg::SetMaxUsers(int nValue)
  175. {
  176. m_nMaxUsers = nValue;
  177. }
  178. /********************************************************************/
  179. /* */
  180. /* Function name : SetPort */
  181. /* Description : Set listening port for new connections */
  182. /* */
  183. /********************************************************************/
  184. void CLYFZReceiveMsg::SetPort(int nValue)
  185. {
  186. m_nPort = nValue;
  187. }
  188. /********************************************************************/
  189. /* */
  190. /* Function name : SetTimeout */
  191. /* Description : Set connection timeout */
  192. /* */
  193. /********************************************************************/
  194. void CLYFZReceiveMsg::SetTimeout(int nValue)
  195. {
  196. m_nTimeout = nValue;
  197. }
  198. /********************************************************************/
  199. /* */
  200. /* Function name : SetTimeout */
  201. /* Description : Set connection timeout */
  202. /* */
  203. /********************************************************************/
  204. void CLYFZReceiveMsg::SetStatisticsInterval(int nValue)
  205. {
  206. m_nStatisticsInterval = nValue;
  207. if (m_nStatisticsInterval != 0)
  208. {
  209. KillTimer(1);
  210. SetTimer(1, nValue, NULL);
  211. }
  212. else
  213. {
  214. KillTimer(1);
  215. }
  216. }
  217. /********************************************************************/
  218. /* */
  219. /* Function name : SetWelcomeMessage */
  220. /* Description : Set welcome message */
  221. /* */
  222. /********************************************************************/
  223. void CLYFZReceiveMsg::SetWelcomeMessage(LPCTSTR lpszText)
  224. {
  225. m_strWelcomeMessage = lpszText;
  226. }
  227. /********************************************************************/
  228. /* */
  229. /* Function name : SetGoodbyeMessage */
  230. /* Description : Set goodbye message */
  231. /* */
  232. /********************************************************************/
  233. void CLYFZReceiveMsg::SetGoodbyeMessage(LPCTSTR lpszText)
  234. {
  235. m_strGoodbyeMessage = lpszText;
  236. }
  237. /********************************************************************/
  238. /* */
  239. /* Function name : Initialize */
  240. /* Description : Initialize event sink. */
  241. /* */
  242. /********************************************************************/
  243. void CLYFZReceiveMsg::Initialize(CFTPEventSink *pEventSink)
  244. {
  245. m_pEventSink = pEventSink;
  246. }
  247. /********************************************************************/
  248. /* */
  249. /* Function name : AddTraceLine */
  250. /* Description : FTP status change. */
  251. /* */
  252. /********************************************************************/
  253. void CLYFZReceiveMsg::AddTraceLine(int nType, LPCTSTR pstrFormat, ...)
  254. {
  255. CString str;
  256. // format and write the data we were given
  257. va_list args;
  258. va_start(args, pstrFormat);
  259. str.FormatV(pstrFormat, args);
  260. m_pEventSink->OnFTPStatusChange(nType, str);
  261. }
  262. /********************************************************************/
  263. /* */
  264. /* Function name : OnThreadStart */
  265. /* Description : Called when a new thread has started. */
  266. /* */
  267. /********************************************************************/
  268. LRESULT CLYFZReceiveMsg::OnThreadStart(WPARAM wParam, LPARAM)
  269. {
  270. m_nConnectionCount++;
  271. m_nTotalConnections++;
  272. CConnectThread *pThread = (CConnectThread *)wParam;
  273. // pThread->m_ConnectSocket.GetPeerName(pThread->m_strRemoteHost, port);
  274. AddTraceLine(0, "[%d] Client connected from %s.", pThread->m_nThreadID, pThread->m_strRemoteHost);
  275. AfxGetMainWnd()->KillTimer (1);
  276. AfxGetMainWnd()->SetTimer(1, 5*60*1000, NULL);
  277. return TRUE;
  278. }
  279. /********************************************************************/
  280. /* */
  281. /* Function name : OnThreadClose */
  282. /* Description : Called when a thread is about to stop. */
  283. /* */
  284. /********************************************************************/
  285. LRESULT CLYFZReceiveMsg::OnThreadClose(WPARAM wParam, LPARAM lParam)
  286. {
  287. m_nConnectionCount--;
  288. CConnectThread *pThread = (CConnectThread *)wParam;
  289. AddTraceLine(0, "[%d] Client disconnected from %s.", pThread->m_nThreadID, pThread->m_strRemoteHost);
  290. m_pEventSink->OnFTPUserDisconnected(pThread->m_nThreadID, pThread->m_ConnectSocket.m_strUserName);
  291. return TRUE;
  292. }
  293. /********************************************************************/
  294. /* */
  295. /* Function name : OnThreadMessage */
  296. /* Description : Message sent from connect connection. */
  297. /* */
  298. /********************************************************************/
  299. LRESULT CLYFZReceiveMsg::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  300. {
  301. switch(wParam)
  302. {
  303. case 0:
  304. m_dwTotalSentBytes += (int)lParam;
  305. break;
  306. case 1:
  307. m_dwTotalReceivedBytes += (int)lParam;
  308. break;
  309. case 2:
  310. switch(lParam)
  311. {
  312. case FTPSTAT_DOWNLOADSUCCEEDED:
  313. m_nFilesDownloaded++;
  314. break;
  315. case FTPSTAT_UPLOADSUCCEEDED:
  316. m_nFilesUploaded++;
  317. break;
  318. case FTPSTAT_DOWNLOADFAILED:
  319. m_nFailedDownloads++;
  320. break;
  321. case FTPSTAT_UPLOADFAILED:
  322. m_nFailedUploads++;
  323. break;
  324. }
  325. break;
  326. default:
  327. break;
  328. }
  329. return TRUE;
  330. }
  331. /********************************************************************/
  332. /* */
  333. /* Function name : CheckMaxUsers */
  334. /* Description : Reached maximum number of connections? */
  335. /* */
  336. /********************************************************************/
  337. BOOL CLYFZReceiveMsg::CheckMaxUsers()
  338. {
  339. if (m_nConnectionCount > m_nMaxUsers)
  340. return TRUE;
  341. else
  342. return FALSE;
  343. }
  344. /********************************************************************/
  345. /* */
  346. /* Function name : OnTimer */
  347. /* Description : Update statictics. */
  348. /* */
  349. /********************************************************************/
  350. void CLYFZReceiveMsg::OnTimer(UINT nIDEvent)
  351. {
  352. // update statictics ?
  353. if (nIDEvent == 1)
  354. {
  355. m_pEventSink->OnFTPSentBytesChange(m_dwTotalSentBytes);
  356. m_pEventSink->OnFTPReceivedBytesChange(m_dwTotalReceivedBytes);
  357. m_pEventSink->OnFTPStatisticChange(0, m_nTotalConnections);
  358. m_pEventSink->OnFTPStatisticChange(1, m_nConnectionCount);
  359. m_pEventSink->OnFTPStatisticChange(2, m_nFilesDownloaded);
  360. m_pEventSink->OnFTPStatisticChange(3, m_nFilesUploaded);
  361. m_pEventSink->OnFTPStatisticChange(4, m_nFailedDownloads);
  362. m_pEventSink->OnFTPStatisticChange(5, m_nFailedUploads);
  363. }
  364. CWnd::OnTimer(nIDEvent);
  365. }
  366. /********************************************************************/
  367. /* */
  368. /* Function name : SetSecurityMode */
  369. /* Description : Set security mode. */
  370. /* */
  371. /********************************************************************/
  372. void CLYFZReceiveMsg::SetSecurityMode(BOOL bBlockSpecific)
  373. {
  374. m_nSecurityMode = bBlockSpecific ? 0 : 1;
  375. }
  376. int FindArray2(CStringArray *pArray, CString Str)
  377. {
  378. for(int i=0; i<pArray->GetSize (); i++)
  379. {
  380. if(pArray->ElementAt (i)==Str)
  381. return i;
  382. }
  383. return -1;
  384. }
  385. extern CStringArray g_ipnoallowarray;
  386. /********************************************************************/
  387. /* */
  388. /* Function name : IsIPAddressAllowed */
  389. /* Description : Check (based on blockmode) if IP is allowed. */
  390. /* */
  391. /********************************************************************/
  392. BOOL CLYFZReceiveMsg::IsIPAddressAllowed(LPCTSTR lpszIPAddress)
  393. {
  394. MyLock lock("IsIPAddressAllowed");
  395. if(::FindArray2 (&g_ipnoallowarray, lpszIPAddress)!=-1)return 0;
  396. return 1;
  397. /* if (m_nSecurityMode == 0)
  398. {
  399. return !m_SecurityManager.IsIPAddressBlocked(lpszIPAddress);
  400. }
  401. else
  402. {
  403. return m_SecurityManager.IsIPAddressNonBlocked(lpszIPAddress);
  404. }*/
  405. }