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

📄 1234dlg.cpp

📁 网络流量伪装算法的设计与实现
💻 CPP
字号:
// 1234Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "1234.h"
#include "1234Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



#ifndef N 
#define N 5 //网络中的节点数。可以改变,为了方便显示定位5个较为适宜。
#endif
#ifndef M
#define M 30 //最大流量
#endif 
#include<math.h>
//**************************************************************************************

int m[N][N]; //该矩阵存储初始流量,并且其它操作都是对它进行操作 
int m2[N][N]; //该矩阵存储初始值,为重新计算保留数据   
int rd[N][N];//该矩阵用来存储,重路由过程的信息

void initialize();  //初始化网络流量矩阵,随机赋值
void rerout();      //路由算法函数,进行重路由操作
void padding(int);  //包填充函数,垃圾包填充使得整个网络流量相等
int sceachmaxf();   //寻找最大流量
void retry();       //矩阵还原,以便重新计算
void oldsf();       //启发式算法的结果

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy1234Dlg dialog

CMy1234Dlg::CMy1234Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy1234Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy1234Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy1234Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy1234Dlg)
	DDX_Control(pDX, IDC_ICON4, m_icon4);
	DDX_Control(pDX, IDC_ICON3, m_icon3);
	DDX_Control(pDX, IDC_ICON2, m_icon2);
	DDX_Control(pDX, IDC_ICON1, m_icon1);
	DDX_Control(pDX, IDC_ICON0, m_icon0);
	DDX_Control(pDX, IDC_pack0, m_pack0);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy1234Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy1234Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BInit, OnBInit)
	ON_BN_CLICKED(IDC_BRerout, OnBRerout)
	ON_BN_CLICKED(IDC_BFillPack, OnBFillPack)
	ON_BN_CLICKED(IDC_BInit3, OnBInit3)
	ON_BN_CLICKED(IDC_BExit, OnBExit)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy1234Dlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy1234Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMy1234Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy1234Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//**************************************************************************************
//流量的初始化函数,由基于时间的WINDOWS随机数产生。
void CMy1234Dlg::OnBInit() 
{
	// TODO: Add your control notification handler code here
 /*
    CRect   rect; 
	m_icon0.GetWindowRect(rect);
	ScreenToClient(&rect);
	rect.top-=20;
	rect.bottom-=20;
	m_pack0.SetWindowPos(NULL,rect.left,rect.top,0,0,SWP_NOSIZE );
	m_pack0.GetWindowRect(rect); 
	ScreenToClient(rect);
	*/
	::srand((unsigned int)time(NULL));
	int i,j;
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
		{
			if(j!=i)
			{
				m[i][j]=rand()%M;
				m2[i][j]=m[i][j];
			} 
			else
				m[i][j]=0;
			rd[i][j]=0;	
		}     
    
	CString csInitPack;
    
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
		{
			csInitPack.Format("%d",m[i][j]);
			GetDlgItem(IDC_pack_00+i*N+j)->SetWindowText(csInitPack);
		}
	 

}

//**************************************************************************************
//垃圾包填充函数
void padding(int mean)
{
	int i,j;
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
			if(j!=i)
				if(m[i][j]<mean)
					m[i][j]=mean;
}

//**************************************************************************************
//寻找最大流量函数
int sceachmaxf()     
{
	int maxflood;
	maxflood=m[0][0];
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			if(maxflood<m[i][j] && j!=i)
				maxflood=m[i][j];
			return maxflood;
}
//**************************************************************************************
//重路由部分,进行3个阶段的重路由
void CMy1234Dlg::OnBRerout() 
{
	// TODO: Add your control notification handler code here
	int  excess_traffic,rerout_qty;
	int all=0;
	int mean=0;
	int left=0;
	int i,j,k;
	//struct reroutlood  pf;
	for(i=0;i<N;i++)
	{
		excess_traffic=rerout_qty=0;
		all=0;
		for(j=0;j<N;j++)
		{
			if(i!=j)
				all+=m[i][j];
		}
		mean=(int)ceil(all/(N-1));

        //********第一步路由,选择最恰当的路径重路由,减少多重转发的几率*********
		for(j=0;j<N;j++)
		{
			if(j!=i && m[i][j]>mean)
				excess_traffic=m[i][j]-mean;             //等待重路由的部分数据
				for(k=0;k<0 && excess_traffic>0;k++)     //选择重路由节点
				{
					if(k!=i && k!=j && m[i][k]<mean && m[k][j]<mean)
					{
						int maxlood=((mean-m[i][k])>(mean-m[k][j]))?(mean-m[k][j]):(mean-m[i][k]);
						//maxlood 为选择路径重路由的较小路径
						rerout_qty=(excess_traffic>maxlood)?maxlood:excess_traffic;
						m[i][k]+=rerout_qty;            //流量发生改变,修改流量矩阵。
						m[k][j]+=rerout_qty;
						excess_traffic-=rerout_qty;     //已经重路由了一部分数据包,修改需要重路由的数据大小
						m[i][j]-=rerout_qty;
						rd[i][j]-=rerout_qty;           //填写重路由过程中的数据变化情况
						rd[i][k]+=rerout_qty;
						rd[k][j]+=rerout_qty;

						
					}    				   
				}

		}
        

		//******第二步路由为“盲目重路由”  完成重路由的目的,不选择路径.*********
		for(j=0;j<N;j++)
		{
            if(j!=i && m[i][j]>mean)
			{
				
				excess_traffic=m[i][j]-mean;                //等待重路由的部分数据
				for(k=N-1;k>=0 && excess_traffic>0;k--)     //选择重路由节点
				{
					if(k!=i && k!=j && m[i][k]<mean)
					{
						rerout_qty=(excess_traffic>(mean-m[i][k]))?
							           (mean-m[i][k]):excess_traffic;
						m[i][k]+=rerout_qty;
						m[k][j]+=rerout_qty;
						excess_traffic-=rerout_qty;
						m[i][j]-=rerout_qty;
						rd[i][j]-=rerout_qty;
						rd[i][k]+=rerout_qty;
						rd[k][j]+=rerout_qty;
					}    				   
				}
			}
		}
		
		
	}

	//****第三步为“修正式的重路由”  解决第二步盲目路由带来的,某些节点流量过大问题*****
	for(i=0;i<N;i++)
	{
		excess_traffic=rerout_qty=0;
		all=0;
		for(j=0;j<N;j++)
		{
			if(i!=j)
				all+=m[i][j];
		}
		mean=(int)ceil(all/(N-1));
		for(j=0;j<N;j++)
		{
            if(j!=i && m[i][j]>mean)
			{
				excess_traffic=m[i][j]-mean;                //等待重路由的部分数据
				for(k=N-1;k>=0 && excess_traffic>0;k--)     //选择重路由节点
				{
					if(k!=i && k!=j && m[i][k]<mean)
					{
						rerout_qty=(excess_traffic>(mean-m[i][k]))?(mean-m[i][k]):excess_traffic;
						m[i][k]+=rerout_qty;
						m[k][j]+=rerout_qty;
						excess_traffic-=rerout_qty;
						m[i][j]-=rerout_qty;
						rd[i][j]-=rerout_qty;
						rd[i][k]+=rerout_qty;
						rd[k][j]+=rerout_qty;
					}    				   
				}
			}
		}		
	}
	//如果经过3步重路由还没有能够解决,重路由带来的某些链路流量超大的情况,则还原矩阵,直接填充。
		if(sceachmaxf()>M-1)
	{
		retry();
		padding(sceachmaxf());
	}
	//*******************输出结果***********************************************
	for(i=0;i<N*N;i++)
	{
        CString csInitPack;
		csInitPack.Format("%d",m[(int)(i/N)][i%N]);
		GetDlgItem(2016+i)->SetWindowText(csInitPack);
	}
	//**************************************************************************
}
//**************************************************************************************
//垃圾包填充函数,把每条链路填充到最大流量.

void CMy1234Dlg::OnBFillPack() 
{
	// TODO: Add your control notification handler code here
    padding(sceachmaxf());
	for(int i=0;i<N*N;i++)
	{
        CString csInitPack;
		csInitPack.Format("%d",m[(int)(i/N)][i%N]);
		GetDlgItem(2016+i)->SetWindowText(csInitPack);
	}
}

void CMy1234Dlg::OnBInit3() 
{
	// TODO: Add your control notification handler code here
	
}

//**************************************************************************************
//退出程序
void CMy1234Dlg::OnBExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}

//**************************************************************************************
//显示重路由过程中,各条链路的流量变化.
void CMy1234Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	for(int i=0;i<N*N;i++)
	{
        CString csInitPack;
		csInitPack.Format("%d",rd[(int)(i/N)][i%N]);
		GetDlgItem(2016+i)->SetWindowText(csInitPack);
	}
	
}
//**************************************************************************************
//矩阵还原,以便重新路由和比较

void retry()
{
	int i,j;
	for (i=0;i<N;i++)
		for(j=0;j<N;j++)
			m[i][j]=m2[i][j];
}

//**************************************************************************************
//矩阵还原按钮
void CMy1234Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	int i,j;
	for (i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			m[i][j]=m2[i][j];
			rd[i][j]=0;
		}

	}

	for(i=0;i<N*N;i++)         //输出结果。
	{
        CString csInitPack;
		csInitPack.Format("%d",m[(int)(i/N)][i%N]);
		GetDlgItem(2016+i)->SetWindowText(csInitPack);
	}
}

//**************************************************************************************
//启发式算法实现。

void CMy1234Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	int i,j,k;	
	int all=0;
	int mean=0;
	int  excess_traffic,rerout_qty;
	retry();
	for(i=0;i<N;i++)
	{
		excess_traffic=rerout_qty=0;
		all=0;
		for(j=0;j<N;j++)
		{
			if(i!=j)
				all+=m[i][j];
		}
		mean=(int)ceil(all/(N-1));
		for(j=0;j<N;j++)
		{
            if(j!=i && m[i][j]>mean)
			{
				
				excess_traffic=m[i][j]-mean;     //等待重路由的部分数据
				for(k=N-1;k>=0 && excess_traffic>0;k--)     //选择重路由节点
				{
					if(k!=i && k!=j && m[i][k]<mean)
					{
						rerout_qty=(excess_traffic>(mean-m[i][k]))?

                                      (mean-m[i][k]):excess_traffic;
						m[i][k]+=rerout_qty;
						m[k][j]+=rerout_qty;
						excess_traffic-=rerout_qty;
						m[i][j]-=rerout_qty;
						rd[i][j]-=rerout_qty;
						rd[i][k]+=rerout_qty;
						rd[k][j]+=rerout_qty;

					}    				   
				}
			}
		}
		
		
	}
	padding(sceachmaxf());
	for(i=0;i<N*N;i++)
	{
        CString csInitPack;
		csInitPack.Format("%d",m[(int)(i/N)][i%N]);
		GetDlgItem(2016+i)->SetWindowText(csInitPack);
	}
	
}

⌨️ 快捷键说明

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