/*
Name: MainFrm.cpp
Copyright: Jes�s Oliva
Author: Jes�s Oliva (jeoliva@telefonica.net)
Date: 25/09/03 20:19
*/
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "FirewallApp.h"
#include "MainFrm.h"
#include "RuleDlg.h"
#include "OptionsDlg.h"
#include "DefaultActionDlg.h"
#include "FirewallAppDoc.h"
#include "FirewallAppView.h"
#include "SockUtil.h"
#include "rules.h"
extern CFirewallAppApp theApp;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_BUTTONSTART, OnButtonstart)
ON_COMMAND(ID_BUTTONADD, OnButtonadd)
ON_COMMAND(ID_BUTTONDEL, OnButtondel)
ON_COMMAND(ID_BUTTONSTOP, OnButtonstop)
ON_UPDATE_COMMAND_UI(ID_BUTTONSTART, OnUpdateButtonstart)
ON_UPDATE_COMMAND_UI(ID_BUTTONSTOP, OnUpdateButtonstop)
ON_COMMAND(IDMENU_ADDRULE, OnMenuAddrule)
ON_COMMAND(IDMENU_DELRULE, OnMenuDelrule)
ON_COMMAND(ID_MENUSTART, OnMenustart)
ON_UPDATE_COMMAND_UI(ID_MENUSTART, OnUpdateMenustart)
ON_COMMAND(ID_MENUSTOP, OnMenustop)
ON_UPDATE_COMMAND_UI(ID_MENUSTOP, OnUpdateMenustop)
ON_COMMAND(ID_APP_EXIT, OnAppExit)
ON_COMMAND(IDMENU_LOADRULES, OnLoadRules)
ON_COMMAND(IDMENU_SAVERULES, OnSaveRules)
ON_COMMAND(IDMENU_SETDEFAULT, OnMenuSetdefault)
ON_COMMAND(ID_MENU_OPTIONS, OnMenuOptions)
//}}AFX_MSG_MAP
ON_COMMAND(IDMENU_EDITRULE, OnEditrule)
ON_UPDATE_COMMAND_UI(IDMENU_EDITRULE, OnUpdateEditrule)
ON_COMMAND(ID_CONTEXTMENU_DOWNRULE, OnContextmenuDownrule)
ON_UPDATE_COMMAND_UI(ID_CONTEXTMENU_DOWNRULE, OnUpdateContextmenuDownrule)
ON_COMMAND(ID_CONTEXTMENU_UPRULE, OnContextmenuUprule)
ON_UPDATE_COMMAND_UI(ID_CONTEXTMENU_UPRULE, OnUpdateContextmenuUprule)
ON_UPDATE_COMMAND_UI(IDMENU_DELRULE, OnUpdateMenuDelrule)
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
started = FALSE;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
// m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
// EnableDocking(CBRS_ALIGN_ANY);
// DockControlBar(&m_wndToolBar);
this->SetWindowText("Firewall PAPI");
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// quit the untitled word in window title
cs.style &= ~ FWS_ADDTOTITLE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnButtonstart()
{
CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
unsigned int i;
DWORD result;
PIP_ADAPTER_INFO pAdapterInfo = NULL, aux;
IP_ADDR_STRING *localIp;
unsigned long len = 0;
// I will send the filter rules to all local ips, because in the rules
// we don't indicate in what interface the rule will be applied
GetAdaptersInfo(pAdapterInfo, &len);
pAdapterInfo = (PIP_ADAPTER_INFO) malloc (len);
result = GetAdaptersInfo(pAdapterInfo, &len);
if(result != ERROR_SUCCESS)
{
CString strError;
strError.LoadString(IDS_ERROR_ADAPTERS);
AfxMessageBox(strError);
return;
}
// Sending rules
for(i=0;i<doc->nRules;i++)
{
// To all adapters
for(aux=pAdapterInfo;aux != NULL;aux=aux->Next)
{
// each ip of a adapter
for(localIp=&aux->IpAddressList;localIp!=NULL;localIp=localIp->Next)
{
pckFilter.AddFilter(CharToIp(localIp->IpAddress.String),
ANY_DIRECTION,
doc->rules[i].sourceIp,
doc->rules[i].sourceMask,
doc->rules[i].destinationIp,
doc->rules[i].destinationMask,
doc->rules[i].sourcePort,
doc->rules[i].destinationPort,
doc->rules[i].protocol);
}
}
}
started = TRUE;
}
void CMainFrame::OnButtonstop()
{
pckFilter.RemoveAll();
started = FALSE;
}
void CMainFrame::OnButtonadd()
{
CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
CRuleDlg dlg;
// check number of rules
if(doc->nRules < MAX_RULES )
{
dlg.defaultAction = pckFilter.GetDefaultAction();
if(dlg.DoModal() == IDOK)
{
// Add the rule to the rule lists
if(doc->AddRule(dlg.srcIp, dlg.srcMask, dlg.srcPort, dlg.dstIp, dlg.dstMask, dlg.dstPort, dlg.protocol, dlg.cAction) != 0)
{
CString strError;
strError.LoadString(IDS_ERROR_ADD_RULE);
AfxMessageBox(strError);
}
else
{
// Then, Update the rules view
CFirewallAppView *view = (CFirewallAppView *)GetActiveView();
view->UpdateList();
}
}
}
else
{
CString strError;
strError.LoadString(IDS_ERROR_MAX_RULES);
AfxMessageBox(strError);
}
}
void CMainFrame::OnButtondel()
{
CFirewallAppView *view = (CFirewallAppView *)GetActiveView();
// Get rule position
POSITION pos = view->m_rules.GetFirstSelectedItemPosition();
if (pos == NULL)
{
CString strError;
strError.LoadString(IDS_ERROR_SELECT_RULE);
AfxMessageBox(strError);
return;
}
int position;
position = view->m_rules.GetNextSelectedItem(pos);
CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
doc->DeleteRule(position);
// Update rules view
view->UpdateList();
}
void CMainFrame::OnUpdateButtonstart(CCmdUI* pCmdUI)
{
if(started)
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
void CMainFrame::OnUpdateButtonstop(CCmdUI* pCmdUI)
{
if(started)
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnMenuAddrule()
{
OnButtonadd();
}
void CMainFrame::OnMenuDelrule()
{
OnButtondel();
}
void CMainFrame::OnMenustart()
{
OnButtonstart();
}
void CMainFrame::OnUpdateMenustart(CCmdUI* pCmdUI)
{
if(started)
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
void CMainFrame::OnMenustop()
{
OnButtonstop();
}
void CMainFrame::OnUpdateMenustop(CCmdUI* pCmdUI)
{
if(started)
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnAppExit()
{
}
void CMainFrame::OnLoadRules()
{
CFirewallAppDoc *doc = (CFirewallAppDoc *)GetActiveDocument();
CFileDialog dg(TRUE,NULL, NULL, OFN_HIDEREADONLY | OFN_CREATEPROMPT,"Rule Files(*.rul)|*.rul|all(*.*)|*.*||", NULL);
if(dg.DoModal()==IDCANCEL)
return;
CString nf=dg.GetPathName();
if(nf.GetLength() == 0)
{
CString strError;
strError.LoadString(IDS_ERROR_FILENAME);
AfxMessageBox(strError);
return;
防火墙源码
4星 · 超过85%的资源 需积分: 9 7 浏览量
2007-08-20
15:29:23
上传
评论
收藏 47KB ZIP 举报
cheer2008
- 粉丝: 8
- 资源: 45
最新资源
- 冯璐阳 42105650—祝福.docx
- 基于多种算法及改进算法实现的移动机器人路径规划matlab源码(含A星算法+PRM+RRT的改进等).zip
- 布里斯托尔纸细分市场、总体规模、先进性、市占率行业分析报告2024年.docx
- Obi绳子插件,好用的很 6.5.4版本
- openjfx-22.0.1-windows-x64-bin-sdk.zip
- 基于ros和stm32f1的小车代码(含串口通信)+项目说明.zip
- 人体姿态估计-基于Tensorflow实现的人体姿态估计算法-附项目源码-优质项目分享.zip
- java实现所有算法大全
- JDBC DAO模式 (复习)
- Proteus仿真AT89C51电子密码锁
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈