MainFrm.cpp
上传用户:wlquartz
上传日期:2022-03-04
资源大小:89k
文件大小:10k
开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "FirewallApp.h"
  5. #include "MainFrm.h"
  6. #include "RuleDlg.h"
  7. #include "FirewallAppDoc.h"
  8. #include "FirewallAppView.h"
  9. #include "SockUtil.h"
  10. #include "rules.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMainFrame
  18. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  19. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  20. //{{AFX_MSG_MAP(CMainFrame)
  21. ON_WM_CREATE()
  22. ON_COMMAND(ID_BUTTONSTART, OnButtonstart)
  23. ON_COMMAND(ID_BUTTONADD, OnButtonadd)
  24. ON_COMMAND(ID_BUTTONDEL, OnButtondel)
  25. ON_COMMAND(ID_BUTTONDESINSTALL, OnButtondesinstall)
  26. ON_COMMAND(ID_BUTTONINSTALL, OnButtoninstall)
  27. ON_COMMAND(ID_BUTTONSTOP, OnButtonstop)
  28. ON_UPDATE_COMMAND_UI(ID_BUTTONSTART, OnUpdateButtonstart)
  29. ON_UPDATE_COMMAND_UI(ID_BUTTONSTOP, OnUpdateButtonstop)
  30. ON_COMMAND(IDMENU_ADDRULE, OnMenuAddrule)
  31. ON_COMMAND(IDMENU_DELRULE, OnMenuDelrule)
  32. ON_COMMAND(IDMENU_INSTALLRULES, OnMenuInstallrules)
  33. ON_COMMAND(IDMENU_UNINSTALLRULES, OnMenuUninstallrules)
  34. ON_COMMAND(ID_MENUSTART, OnMenustart)
  35. ON_UPDATE_COMMAND_UI(ID_MENUSTART, OnUpdateMenustart)
  36. ON_COMMAND(ID_MENUSTOP, OnMenustop)
  37. ON_UPDATE_COMMAND_UI(ID_MENUSTOP, OnUpdateMenustop)
  38. ON_COMMAND(ID_APP_EXIT, OnAppExit)
  39. ON_COMMAND(IDMENU_LOADRULES, OnLoadRules)
  40. ON_COMMAND(IDMENU_SAVERULES, OnSaveRules)
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. static UINT indicators[] =
  44. {
  45. ID_SEPARATOR,           // status line indicator
  46. ID_INDICATOR_CAPS,
  47. ID_INDICATOR_NUM,
  48. ID_INDICATOR_SCRL,
  49. };
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMainFrame construction/destruction
  52. CMainFrame::CMainFrame()
  53. {
  54. started = FALSE;
  55. // Cargo el driver IpFilterDriver y lo pongo en estado Start.
  56. ipFltDrv.LoadDriver("IpFilterDriver", "System32\Drivers\IpFltDrv.sys", NULL, TRUE);
  57. // No debo desregistrar el driver una vez salgo de la aplicacion.
  58. ipFltDrv.SetRemovable(FALSE);
  59. // Cargo el driver de filtrado
  60. if(filterDriver.LoadDriver("DrvFltIp", NULL, NULL, TRUE) != DRV_SUCCESS)
  61. {
  62. AfxMessageBox("Error loading the driver.");
  63. exit(-1);
  64. }
  65. }
  66. CMainFrame::~CMainFrame()
  67. {
  68. }
  69. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  70. {
  71. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  72. return -1;
  73. if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  74. | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  75. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  76. {
  77. TRACE0("Failed to create toolbarn");
  78. return -1;      // fail to create
  79. }
  80. if (!m_wndStatusBar.Create(this) ||
  81. !m_wndStatusBar.SetIndicators(indicators,
  82.   sizeof(indicators)/sizeof(UINT)))
  83. {
  84. TRACE0("Failed to create status barn");
  85. return -1;      // fail to create
  86. }
  87. // TODO: Delete these three lines if you don't want the toolbar to
  88. //  be dockable
  89. // m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  90. // EnableDocking(CBRS_ALIGN_ANY);
  91. // DockControlBar(&m_wndToolBar);
  92. SetWindowText("Firewall FHK");
  93. return 0;
  94. }
  95. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  96. {
  97. if( !CFrameWnd::PreCreateWindow(cs) )
  98. return FALSE;
  99. //para quitar el untitled del titulo de la aplicacion
  100. cs.style &= ~ FWS_ADDTOTITLE;
  101. return TRUE;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CMainFrame diagnostics
  105. #ifdef _DEBUG
  106. void CMainFrame::AssertValid() const
  107. {
  108. CFrameWnd::AssertValid();
  109. }
  110. void CMainFrame::Dump(CDumpContext& dc) const
  111. {
  112. CFrameWnd::Dump(dc);
  113. }
  114. #endif //_DEBUG
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CMainFrame message handlers
  117. void CMainFrame::OnButtonstart() 
  118. {
  119. if(filterDriver.WriteIo(START_IP_HOOK, NULL, 0) != DRV_ERROR_IO)
  120. {
  121. started = TRUE;
  122. }
  123. }
  124. void CMainFrame::OnButtonstop() 
  125. {
  126. if(filterDriver.WriteIo(STOP_IP_HOOK, NULL, 0) != DRV_ERROR_IO)
  127. {
  128. started = FALSE;
  129. }
  130. }
  131. void CMainFrame::OnButtonadd() 
  132. {
  133. CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
  134. CRuleDlg dlg;
  135. // Compruebo que no he llegado al limite de numero de reglas
  136. if(doc->nRules < MAX_RULES )
  137. {
  138. if(dlg.DoModal() == IDOK)
  139. {
  140. // Primero a馻do la regla a mi lista de reglas
  141. if(doc->AddRule(dlg.srcIp, dlg.srcMask, dlg.srcPort, dlg.dstIp, dlg.dstMask, dlg.dstPort, dlg.protocol, dlg.cAction) != 0)
  142. AfxMessageBox("Error adding the rule");
  143. else
  144. {
  145. // Actualizo el control de visualizacion de reglas
  146. CFirewallAppView *view = (CFirewallAppView *)GetActiveView();
  147. view->UpdateList();
  148. }
  149. }
  150. }
  151. else
  152. AfxMessageBox("No more Rules are allowed.");
  153. }
  154. void CMainFrame::OnButtondel() 
  155. {
  156. CFirewallAppView *view = (CFirewallAppView *)GetActiveView();
  157. // Veo la posici髇 de la regla en la lista
  158. POSITION pos = view->m_rules.GetFirstSelectedItemPosition();
  159. if (pos == NULL)
  160. {
  161. AfxMessageBox("Select a Rule, please.");
  162. return;
  163. }
  164. int position;
  165. position = view->m_rules.GetNextSelectedItem(pos);
  166. CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
  167. doc->DeleteRule(position);
  168. // Actualizo la vista
  169. view->UpdateList();
  170. }
  171. void CMainFrame::OnButtoninstall() 
  172. {
  173. CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
  174. unsigned int i;
  175. DWORD result;
  176. // Le envio las reglas una a una al driver
  177. for(i=0;i<doc->nRules;i++)
  178. {
  179. result = AddFilterToFw(doc->rules[i].sourceIp, 
  180.    doc->rules[i].sourceMask,
  181.    doc->rules[i].sourcePort,
  182.    doc->rules[i].destinationIp,
  183.    doc->rules[i].destinationMask,
  184.    doc->rules[i].destinationPort,
  185.    doc->rules[i].protocol, 
  186.    doc->rules[i].action);
  187. if (!result) 
  188. break;
  189. }
  190. }
  191. void CMainFrame::OnButtondesinstall() 
  192. {
  193. if(filterDriver.WriteIo(CLEAR_FILTER, NULL, 0) == DRV_ERROR_IO)
  194. {
  195. AfxMessageBox("Error uninstalling rules.");
  196. }
  197. }
  198. void CMainFrame::OnUpdateButtonstart(CCmdUI* pCmdUI) 
  199. {
  200. if(started)
  201. pCmdUI->Enable(FALSE);
  202. else
  203. pCmdUI->Enable(TRUE);
  204. }
  205. void CMainFrame::OnUpdateButtonstop(CCmdUI* pCmdUI) 
  206. {
  207. if(started)
  208. pCmdUI->Enable(TRUE);
  209. else
  210. pCmdUI->Enable(FALSE);
  211. }
  212. void CMainFrame::OnMenuAddrule() 
  213. {
  214. OnButtonadd();
  215. }
  216. void CMainFrame::OnMenuDelrule() 
  217. {
  218. OnButtondel();
  219. }
  220. void CMainFrame::OnMenuInstallrules() 
  221. {
  222. OnButtoninstall();
  223. }
  224. void CMainFrame::OnMenuUninstallrules() 
  225. {
  226. OnButtondesinstall();
  227. }
  228. void CMainFrame::OnMenustart() 
  229. {
  230. OnButtonstart();
  231. }
  232. void CMainFrame::OnUpdateMenustart(CCmdUI* pCmdUI) 
  233. {
  234. if(started)
  235. pCmdUI->Enable(FALSE);
  236. else
  237. pCmdUI->Enable(TRUE);
  238. }
  239. void CMainFrame::OnMenustop() 
  240. {
  241. OnButtonstop();
  242. }
  243. void CMainFrame::OnUpdateMenustop(CCmdUI* pCmdUI) 
  244. {
  245. if(started)
  246. pCmdUI->Enable(TRUE);
  247. else
  248. pCmdUI->Enable(FALSE);
  249. }
  250. void CMainFrame::OnAppExit() 
  251. {
  252. }
  253. BOOL CMainFrame::AddFilterToFw(unsigned long srcIp, 
  254.    unsigned long srcMask,
  255.    unsigned short srcPort, 
  256.    unsigned long dstIp,
  257.    unsigned long dstMask,
  258.    unsigned short dstPort,
  259.    unsigned int protocol,
  260.    int action)
  261. {
  262. IPFilter pf;
  263. pf.protocol = protocol;
  264. pf.destinationIp = dstIp;
  265. pf.sourceIp  = srcIp;
  266. pf.destinationMask = dstMask;
  267. pf.sourceMask    = srcMask;
  268. pf.destinationPort = htons(dstPort);
  269. pf.sourcePort    = htons(srcPort);
  270. pf.drop = action;
  271. // Envio la regla al driver
  272. DWORD result = filterDriver.WriteIo(ADD_FILTER, &pf, sizeof(pf));
  273. if (result != DRV_SUCCESS) 
  274. {
  275. AfxMessageBox("Error adding the rule.");
  276. return FALSE;
  277. }
  278. else
  279. return TRUE;
  280. }
  281. void CMainFrame::OnLoadRules() 
  282. {
  283. CFile file;
  284. CFileException e;
  285. DWORD nRead;
  286. CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
  287. CFileDialog dg(TRUE,NULL, NULL, OFN_HIDEREADONLY | OFN_CREATEPROMPT,"Rule Files(*.rul)|*.rul|all(*.*)|*.*||", NULL);
  288. if(dg.DoModal()==IDCANCEL)
  289. return;
  290. CString nf=dg.GetPathName();
  291. if(nf.GetLength() == 0)
  292. {
  293. AfxMessageBox("This file name isn't valid");
  294. return;
  295. }
  296. if( !file.Open(nf, CFile::modeRead, &e ) )
  297. {
  298. AfxMessageBox("Error openning the file.");
  299. return;
  300. }
  301. doc->ResetRules();
  302. RuleInfo rule;
  303.     do
  304.     {
  305.   nRead = file.Read(&rule, sizeof(RuleInfo));
  306. if(nRead == 0)
  307. break;
  308. if(doc->AddRule(rule.sourceIp,
  309.         rule.sourceMask,
  310. rule.sourcePort,
  311. rule.destinationIp,
  312. rule.destinationMask,
  313. rule.destinationPort,
  314. rule.protocol,    
  315. rule.action)  != 0)
  316. {
  317. AfxMessageBox("Error adding the rule.");
  318. break;
  319. }
  320.     }while (1);
  321. // Actualizo el control de visualizacion de reglas
  322. CFirewallAppView *view = (CFirewallAppView *)GetActiveView();
  323. view->UpdateList();
  324. }
  325. void CMainFrame::OnSaveRules() 
  326. {
  327. CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
  328. if(doc->nRules == 0)
  329. {
  330. AfxMessageBox("There aren't rules");
  331. return;
  332. }
  333. CFileDialog dg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_CREATEPROMPT,"Rule Files(*.rul)|*.rul|all(*.*)|*.*||", NULL);
  334. if(dg.DoModal()==IDCANCEL)
  335. return;
  336. CString nf=dg.GetPathName();
  337. if(nf.GetLength() == 0)
  338. {
  339. AfxMessageBox("This file name isn't valid");
  340. return;
  341. }
  342. CFile file;
  343. CFileException e;
  344. if( !file.Open( nf, CFile::modeCreate | CFile::modeWrite, &e ) )
  345. {
  346. AfxMessageBox("Error openning the file.");
  347. return;
  348. }
  349. unsigned int i;
  350. // Le envio las reglas una a una al driver
  351. for(i=0;i<doc->nRules;i++)
  352. {
  353. file.Write(&doc->rules[i], sizeof(RuleInfo));
  354. }
  355. file.Close();
  356. }