CPhotoFTPReceive.cpp 12 KB

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