ftpdDlg.cpp
上传用户:shengde
上传日期:2007-02-26
资源大小:117k
文件大小:8k
源码类别:

Ftp服务器

开发平台:

Visual C++

  1. // ftpdDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ftpd.h"
  5. #include "ftpdDlg.h"
  6. #include "CFtpd.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. CList<CFtpd*,CFtpd*> FTPDList;
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CFtpdDlg dialog
  50. CFtpdDlg::CFtpdDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CFtpdDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CFtpdDlg)
  54. m_ftpserver_name = _T("");
  55. m_ftpd_port = 0;
  56. m_rootdir = _T("");
  57. m_b_ipchk = FALSE;
  58. m_b_ipchk_pasv = FALSE;
  59. m_b_switch_pasv_ip = FALSE;
  60. m_specify_data_port = FALSE;
  61. m_port_from = 0;
  62. m_port_to = 0;
  63. m_b_auto_choose_ip = FALSE;
  64. //}}AFX_DATA_INIT
  65. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  66. }
  67. void CFtpdDlg::DoDataExchange(CDataExchange* pDX)
  68. {
  69. CDialog::DoDataExchange(pDX);
  70. //{{AFX_DATA_MAP(CFtpdDlg)
  71. DDX_Control(pDX, IDC_TREE1, m_ftpdtree);
  72. DDX_Control(pDX, IDC_COMBO1, m_ip_combo_ctrl);
  73. DDX_Text(pDX, IDC_EDIT1, m_ftpserver_name);
  74. DDV_MaxChars(pDX, m_ftpserver_name, 32);
  75. DDX_Text(pDX, IDC_EDIT2, m_ftpd_port);
  76. DDX_Text(pDX, IDC_EDIT3, m_rootdir);
  77. DDX_Check(pDX, IDC_CHECK2, m_b_ipchk);
  78. DDX_Check(pDX, IDC_CHECK3, m_b_ipchk_pasv);
  79. DDX_Check(pDX, IDC_CHECK1, m_b_switch_pasv_ip);
  80. DDX_Check(pDX, IDC_CHECK4, m_specify_data_port);
  81. DDX_Text(pDX, IDC_EDIT4, m_port_from);
  82. DDX_Text(pDX, IDC_EDIT5, m_port_to);
  83. DDX_Check(pDX, IDC_CHECK5, m_b_auto_choose_ip);
  84. //}}AFX_DATA_MAP
  85. }
  86. BEGIN_MESSAGE_MAP(CFtpdDlg, CDialog)
  87. //{{AFX_MSG_MAP(CFtpdDlg)
  88. ON_WM_SYSCOMMAND()
  89. ON_WM_PAINT()
  90. ON_WM_QUERYDRAGICON()
  91. ON_COMMAND(ID_MENUITEM32771, OnLOAD)
  92. ON_COMMAND(ID_MENUITEM32772, OnSAVE)
  93. ON_WM_CREATE()
  94. ON_BN_CLICKED(IDC_BUTTON2, OnLaunch)
  95. //}}AFX_MSG_MAP
  96. END_MESSAGE_MAP()
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CFtpdDlg message handlers
  99. BOOL CFtpdDlg::OnInitDialog()
  100. {
  101. CDialog::OnInitDialog();
  102. // Add "About..." menu item to system menu.
  103. // IDM_ABOUTBOX must be in the system command range.
  104. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  105. ASSERT(IDM_ABOUTBOX < 0xF000);
  106. CMenu* pSysMenu = GetSystemMenu(FALSE);
  107. if (pSysMenu != NULL)
  108. {
  109. CString strAboutMenu;
  110. strAboutMenu.LoadString(IDS_ABOUTBOX);
  111. if (!strAboutMenu.IsEmpty())
  112. {
  113. pSysMenu->AppendMenu(MF_SEPARATOR);
  114. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  115. }
  116. }
  117. GetCurrentDirectory(255,AppDir);
  118. wsprintf(userfile,"%s\fsftpd.user",AppDir);
  119. wsprintf(ipfile,"%s\fsftpd.allow",AppDir);
  120. SetIcon(m_hIcon, TRUE); // Set big icon
  121. SetIcon(m_hIcon, FALSE); // Set small icon
  122. int i;
  123. char sTmp[32]="";
  124. unsigned long IP;
  125. IP=-1;
  126. CFtpd tmpd;
  127. for (i=0; IP!=0;i++) 
  128. {
  129. IP=tmpd.GetLocalNumericIP(i);
  130. if (IP !=0) 
  131. {
  132. wsprintf(sTmp,"%d.%d.%d.%d",(IP&0xFF),((IP>>8 ) & 0xFF),((IP>>16) & 0xFF),((IP>>24) & 0xFF));
  133. m_ip_combo_ctrl.AddString(sTmp);
  134. }
  135. }
  136. return TRUE;  // return TRUE  unless you set the focus to a control
  137. }
  138. void CFtpdDlg::OnSysCommand(UINT nID, LPARAM lParam)
  139. {
  140. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  141. {
  142. CAboutDlg dlgAbout;
  143. dlgAbout.DoModal();
  144. }
  145. else
  146. {
  147. CDialog::OnSysCommand(nID, lParam);
  148. }
  149. }
  150. void CFtpdDlg::OnPaint() 
  151. {
  152. if (IsIconic())
  153. {
  154. CPaintDC dc(this); // device context for painting
  155. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  156. // Center icon in client rectangle
  157. int cxIcon = GetSystemMetrics(SM_CXICON);
  158. int cyIcon = GetSystemMetrics(SM_CYICON);
  159. CRect rect;
  160. GetClientRect(&rect);
  161. int x = (rect.Width() - cxIcon + 1) / 2;
  162. int y = (rect.Height() - cyIcon + 1) / 2;
  163. // Draw the icon
  164. dc.DrawIcon(x, y, m_hIcon);
  165. }
  166. else
  167. {
  168. CDialog::OnPaint();
  169. }
  170. }
  171. HCURSOR CFtpdDlg::OnQueryDragIcon()
  172. {
  173. return (HCURSOR) m_hIcon;
  174. }
  175. void CFtpdDlg::OnLOAD() 
  176. {
  177. char item[64] = "";
  178. CFileDialog cf(TRUE, "", "*.ftpd");;
  179. cf.DoModal();
  180. CString fullpath = cf.GetPathName();
  181. GetPrivateProfileString("FTPD", "SERVER_NAME",NULL,item,256,fullpath);
  182. m_ftpserver_name = item;
  183. GetPrivateProfileString("FTPD", "IPCHK_CONNECTION",NULL,item,256,fullpath);
  184. m_b_ipchk = atoi(item);
  185. GetPrivateProfileString("FTPD", "IPCHK_DATA",NULL,item,256,fullpath);
  186. m_b_ipchk_pasv = atoi(item);
  187. GetPrivateProfileString("FTPD", "PASV_IP_ROLLING",NULL,item,256,fullpath);
  188. m_b_switch_pasv_ip = atoi(item);
  189. GetPrivateProfileString("FTPD", "AUTO_CHOOSE_IP",NULL,item,256,fullpath);
  190. m_b_auto_choose_ip = atoi(item);
  191. GetPrivateProfileString("FTPD", "LISTEN_PORT",NULL,item,256,fullpath);
  192. m_ftpd_port = atoi(item);
  193. GetPrivateProfileString("FTPD", "USE_DATAPORT_RANGE",NULL,item,256,fullpath);
  194. m_specify_data_port = atoi(item);
  195. GetPrivateProfileString("FTPD", "PORT_FROM",NULL,item,256,fullpath);
  196. m_port_from = atoi(item);
  197. GetPrivateProfileString("FTPD", "PORT_TO",NULL,item,256,fullpath);
  198. m_port_to = atoi(item);
  199. GetPrivateProfileString("FTPD", "ROOT_PATH",NULL,item,256,fullpath);
  200. m_rootdir = item;
  201. UpdateData(FALSE);
  202. }
  203. void CFtpdDlg::OnSAVE() 
  204. {
  205. char item[64] = "";
  206. CFileDialog cf(TRUE, "", "*.ftpd");;
  207. cf.DoModal();
  208. CString fullpath = cf.GetPathName();
  209. wsprintf(item,"%s",m_ftpserver_name);
  210. WritePrivateProfileString("FTPD", "SERVER_NAME",item,(LPCSTR)fullpath);
  211. wsprintf(item,"%d",m_b_ipchk);
  212. WritePrivateProfileString("FTPD", "IPCHK_CONNECTION",item,(LPCSTR)fullpath);
  213. wsprintf(item,"%d",m_b_ipchk_pasv);
  214. WritePrivateProfileString("FTPD", "IPCHK_DATA",item,(LPCSTR)fullpath);
  215. wsprintf(item,"%d",m_b_switch_pasv_ip);
  216. WritePrivateProfileString("FTPD", "PASV_IP_ROLLING",item,(LPCSTR)fullpath);
  217. wsprintf(item,"%d",m_b_auto_choose_ip);
  218. WritePrivateProfileString("FTPD", "AUTO_CHOOSE_IP",item,(LPCSTR)fullpath);
  219. wsprintf(item,"%d",m_ftpd_port);
  220. WritePrivateProfileString("FTPD", "LISTEN_PORT",item,(LPCSTR)fullpath);
  221. wsprintf(item,"%s",(LPCSTR)m_rootdir);
  222. WritePrivateProfileString("FTPD", "ROOT_PATH",item,(LPCSTR)fullpath);
  223. wsprintf(item,"%d",m_specify_data_port);
  224. WritePrivateProfileString("FTPD", "USE_DATAPORT_RANGE",item,(LPCSTR)fullpath);
  225. wsprintf(item,"%d",m_port_from);
  226. WritePrivateProfileString("FTPD", "PORT_FROM",item,(LPCSTR)fullpath);
  227. wsprintf(item,"%d",m_port_to);
  228. WritePrivateProfileString("FTPD", "PORT_TO",item,(LPCSTR)fullpath);
  229. }
  230. int CFtpdDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  231. {
  232. if (CDialog::OnCreate(lpCreateStruct) == -1)
  233. return -1;
  234. return 1;
  235. }
  236. void CFtpdDlg::OnLaunch() 
  237. {
  238. char sadr[32] = "";
  239. UpdateData(TRUE);
  240. CFtpd* Ftpdsock = new CFtpd();
  241. Ftpdsock->m_servername = m_ftpserver_name;
  242.     Ftpdsock->b_ipchk = (bool) m_b_ipchk;
  243. Ftpdsock->b_ipchk_pasv = (bool) m_b_ipchk_pasv;
  244. Ftpdsock->b_switch_pasv_ip = (bool) m_b_switch_pasv_ip;
  245. Ftpdsock->b_auto_choose_ip = (bool) m_b_auto_choose_ip;
  246. Ftpdsock->m_port = m_ftpd_port;
  247. Ftpdsock->m_specify_data_port = (bool) m_specify_data_port;
  248. Ftpdsock->m_port_from = m_port_from;
  249. Ftpdsock->m_port_to = m_port_to;
  250. strcpy(Ftpdsock->userfile,userfile);
  251. strcpy(Ftpdsock->ipfile,ipfile);
  252. Ftpdsock->Create(m_ftpd_port,SOCK_STREAM,sadr);
  253. strcpy(Ftpdsock->m_root_dir,(LPCSTR)m_rootdir);
  254. Ftpdsock->Listen(32);
  255. FTPDList.AddTail(Ftpdsock);
  256. }