⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firewallidedlg.cpp

📁 简单防火墙功能程序 开发平台: windows XP SP2 Visual Studio.net 2003 本代码已删除资源文件
💻 CPP
字号:
// FireWallIDEDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "FireWallIDE.h"
#include "FireWallIDEDlg.h"
#include ".\firewallidedlg.h"
#include "SetFilterDialog.h"
#include <Windows.h>
#include <tlhelp32.h>
#include <Iphlpapi.h>
#include <psapi.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#pragma comment(lib,"iphlpapi.lib")
#pragma comment(lib,"advapi32.lib")
#pragma comment(lib,"user32.lib")
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
#pragma comment(lib,"psapi.lib")

#define WM_TRAYICONCLICK (WM_USER+117) 
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CFireWallIDEDlg 对话框



CFireWallIDEDlg::CFireWallIDEDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFireWallIDEDlg::IDD, pParent)
	, nCurrentLogNo(0)
	, nFilterNumber(0)
	, strStateInfo(_T(""))
	, bFilting(false)
{
	
	memset(this->FilterArray,0,sizeof(this->FilterArray));
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	WSADATA wsaData;
	WORD wVersionRequested = MAKEWORD(1, 1);
	::WSAStartup(wVersionRequested, &wsaData);
	PfCreateInterface(0, 
                     PF_ACTION_FORWARD,
                     PF_ACTION_FORWARD,
                     FALSE, 
                     TRUE, 
                     &s_hIf);
}
CFireWallIDEDlg::~CFireWallIDEDlg()
{
	if(this->bFilting==true)
	{
		PfUnBindInterface( s_hIf );
	}
    PfDeleteInterface( s_hIf );
	WSACleanup();
}
void CFireWallIDEDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDOK, CButtonStart);
	DDX_Control(pDX, IDSTOP, CButtonStop);
	DDX_Control(pDX, IDCANCEL2, CButtonSetFilter);
	DDX_Control(pDX, IDC_LIST3, CListFilterCtrl);
	DDX_Text(pDX, IDC_EDIT1, strStateInfo);
}

BEGIN_MESSAGE_MAP(CFireWallIDEDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, OnBnClickedStartFilter)
	ON_BN_CLICKED(IDSTOP, OnBnClickedStopFilter)
	ON_BN_CLICKED(IDCANCEL2, OnBnClickedSetFilter)
	ON_WM_TIMER()
	ON_MESSAGE(WM_TRAYICONCLICK, OnTrayIconClick)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_EXITPROCESS, OnBnClickedExitprocess)
END_MESSAGE_MAP()


// CFireWallIDEDlg 消息处理程序

BOOL CFireWallIDEDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	this->CButtonStop.EnableWindow(FALSE);
	this->strStateInfo.Format("准备");
	this->UpdateData(false);


	//初始化FilterList
	this->CListFilterCtrl.SetFont(&m_font);

	this->CListFilterCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	this->CListFilterCtrl.SetBkColor(RGB(247,247,255));
	this->CListFilterCtrl.SetTextColor(RGB(0,0,255));
	this->CListFilterCtrl.SetTextBkColor(RGB(247,247,255));
	this->CListFilterCtrl.InsertColumn(0, " 协议", LVCFMT_CENTER, 50);
	this->CListFilterCtrl.InsertColumn(1, "源IP地址", LVCFMT_CENTER, 140);
	this->CListFilterCtrl.InsertColumn(2, "目的IP地址", LVCFMT_CENTER, 140);
	this->CListFilterCtrl.InsertColumn(3, "源端口号", LVCFMT_CENTER, 96);
	this->CListFilterCtrl.InsertColumn(4, "目的端口号", LVCFMT_CENTER, 96);

	dwLocalIP=0;
	PIP_ADAPTER_INFO pAdapterInfo = NULL;
	ULONG ulLen = 0;
	::GetAdaptersInfo(pAdapterInfo,&ulLen);
	pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulLen);
	if(::GetAdaptersInfo(pAdapterInfo,&ulLen) ==  ERROR_SUCCESS)
	{
		if(pAdapterInfo != NULL)
		{
			dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
			
			return TRUE; 
		}
		
	}
	this->MessageBox("Error !!! ");
	 // 除非设置了控件的焦点,否则返回 TRUE
	return TRUE; 
}

LRESULT CFireWallIDEDlg::OnTrayIconClick(WPARAM wparm, LPARAM lparm)
{
    if(lparm==WM_LBUTTONDOWN) 
    {
        ShowWindow(SW_RESTORE);
		this->SetForegroundWindow();
		Shell_NotifyIcon(NIM_DELETE, &nid);
    }
	return 0;
}
void CFireWallIDEDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		if ((nID & 0xfff0) == SC_CLOSE)
		{			    
			HICON hIcon = LoadIcon((AfxGetApp()->m_hInstance), MAKEINTRESOURCE(IDR_MAINFRAME));
			char tip[255];
			wsprintf(tip, "BlackBeast Persional FireWall");
			nid.cbSize = sizeof(NOTIFYICONDATA);
			nid.hWnd = this->m_hWnd;
			nid.uID = IDR_MAINFRAME;
			nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
			nid.uCallbackMessage = WM_TRAYICONCLICK;
			nid.hIcon = m_hIcon;
			if (tip)
			{
				lstrcpyn(nid.szTip, tip, sizeof(nid.szTip));
			}
			else
			{
				nid.szTip[0] = '\0';
			}
    //添加入任务栏
			Shell_NotifyIcon(NIM_ADD, &nid);
			if (m_hIcon)
			{
				DestroyIcon(m_hIcon);
			}
			CDialog::OnSysCommand(nID, lParam);
			ShowWindow(SW_HIDE);
			return;
		}
		else
		{
			CDialog::OnSysCommand(nID, lParam);
		}
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CFireWallIDEDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CFireWallIDEDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CFireWallIDEDlg::OnBnClickedStartFilter()
{
	// TODO: 在此添加控件通知处理程序代码
	if(this->nFilterNumber==0)
	{
		this->MessageBox("没有设置过滤条件,防火墙无法启动!","Warning",MB_OK|MB_ICONWARNING);
	}
	else
	{
		this->CButtonStop.EnableWindow();
		this->CButtonStart.EnableWindow(false);
		this->bFilting=true;
		this->strStateInfo.Format("防火墙开始工作");
		this->UpdateData(false);
	
		if(NO_ERROR!= PfBindInterfaceToIPAddress(s_hIf, PF_IPV4, (PBYTE)&dwLocalIP))
		{
			this->MessageBox("PfBindInterfaceToIPAddress Error ");
		}
		for(int i=0;i<this->nFilterNumber;i++)
		{
			FILTER_HANDLE fHandle;
			PF_FILTER_DESCRIPTOR inFilter;
			inFilter.dwFilterFlags = FD_FLAGS_NOSYN; //一直添这个值
			inFilter.dwRule = 0; //一直添这个值
			inFilter.pfatType = PF_IPV4; //用 ipV4 地址
			inFilter.SrcAddr = (PBYTE)&(this->FilterArray[i].ulSourceIP); //设置本地IP地址
			inFilter.SrcMask = (PBYTE)"\xff\xff\xff\xff"; //设置本地子网掩码

			inFilter.wSrcPort = this->FilterArray[i].usSourcePort==0?FILTER_TCPUDP_PORT_ANY:ntohs(this->FilterArray[i].usSourcePort);
			inFilter.wSrcPortHighRange = inFilter.wSrcPort ;

			if(this->FilterArray[i].ulDestIP==0)
			{
				inFilter.DstAddr = (PBYTE)&(this->FilterArray[i].ulDestIP); 
				inFilter.DstMask = (PBYTE)&(this->FilterArray[i].ulDestIP); 
			}
			else
			{
				inFilter.DstAddr = (PBYTE)&(this->FilterArray[i].ulDestIP); 
				inFilter.DstMask = (PBYTE)"\xff\xff\xff\xff";
			}
			inFilter.wDstPort = this->FilterArray[i].usDestPort==0?FILTER_TCPUDP_PORT_ANY:ntohs(this->FilterArray[i].usDestPort); 
			inFilter.wDstPortHighRange =inFilter.wDstPort;
			inFilter.fLateBound = LB_SRC_ADDR_USE_DSTADDR_FLAG|LB_DST_ADDR_USE_DSTADDR_FLAG;

			inFilter.dwProtocol = this->FilterArray[i].uProtocol==6?FILTER_PROTO_TCP:FILTER_PROTO_UDP; 
			if(NO_ERROR!=PfAddFiltersToInterface(s_hIf, 0, NULL,1, &inFilter,  &fHandle))
			{
				this->MessageBox("Error");
			}
		}
			
	}
}

void CFireWallIDEDlg::OnBnClickedStopFilter()
{
	// TODO: 在此添加控件通知处理程序代码
	this->strStateInfo.Format("防火墙停止工作");
	this->UpdateData(false);
	this->CButtonStart.EnableWindow();
	this->CButtonStop.EnableWindow(false);
	this->bFilting=false;
	PfUnBindInterface( s_hIf );
}

void CFireWallIDEDlg::OnBnClickedSetFilter()
{
	// TODO: 在此添加控件通知处理程序代码
	bool bFilting=this->bFilting;
	if(bFilting==true)
	{
		this->OnBnClickedStopFilter();
	}
	this->CListFilterCtrl.DeleteAllItems();
	this->nFilterNumber=0;
	bool bContinueAddFilter=false;
	do
	{
		CSetFilterDialog theSetFilterDlg;
		if(theSetFilterDlg.DoModal()==IDOK )
		{
			bContinueAddFilter=false;
		}
		else
		{
			bContinueAddFilter=true;
		}
		this->AddFilter(&(theSetFilterDlg.CurrentFilterEntry));
		if(this->nFilterNumber==19)
		{
			this->MessageBox("Too many Filter!");
			break;
		}
	}while(bContinueAddFilter);
	
	if(bFilting==true)
	{
		this->OnBnClickedStartFilter();
	}
	this->strStateInfo.Format("设置过滤器完成");
	this->UpdateData(false);
	
}


void CFireWallIDEDlg::AddFilter(const FilterEntry* pFilterEntry)
{
	memcpy(&(this->FilterArray[this->nFilterNumber] ),pFilterEntry,sizeof(FilterEntry));
	this->FilterArray[this->nFilterNumber].ulSourceIP=this->dwLocalIP;
	if(pFilterEntry->uProtocol==6)
	{
		this->CListFilterCtrl.InsertItem(this->nFilterNumber,"Tcp");
	}
	else
	{
		if(pFilterEntry->uProtocol==17)
		{
			this->CListFilterCtrl.InsertItem(this->nFilterNumber,"Udp");
		}
		else
		{
			this->CListFilterCtrl.InsertItem(this->nFilterNumber,"Unknow");
		}
	}
	in_addr DestIp,SourceIp;
	
	
	if(pFilterEntry->ulSourceIP!=0)
	{
		this->MessageBox("Error");
	}
	else
	{
		SourceIp.S_un.S_addr=this->dwLocalIP;
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,1,inet_ntoa(SourceIp));
	}
	if(pFilterEntry->ulDestIP!=0)
	{
		DestIp.S_un.S_addr=pFilterEntry->ulDestIP;
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,2,inet_ntoa(DestIp));
	}
	else
	{
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,2,"任意");
	}
	char stBuf[512];
	if(pFilterEntry->usSourcePort!=0)
	{
		sprintf(stBuf,"%u",ntohs(pFilterEntry->usSourcePort));
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,3,stBuf);
	}
	else
	{
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,3,"任意");
	}
	if(pFilterEntry->usDestPort!=0)
	{
		sprintf(stBuf,"%u",ntohs(pFilterEntry->usDestPort));
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,4,stBuf);
	}
	else
	{
		this->CListFilterCtrl.SetItemText(this->nFilterNumber,4,"任意");
	}
	this->nFilterNumber++;
}


void CFireWallIDEDlg::OnClose()
{	
}

void CFireWallIDEDlg::OnBnClickedExitprocess()
{
	// TODO: 在此添加控件通知处理程序代码
	if(this->bFilting==true)
	{
		
	}
	Shell_NotifyIcon(NIM_DELETE, &nid);
	EndDialog(0);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -