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

📄 readip0802dlg.cpp

📁 VC写的分析IP数据包的代码.rar如何使用的朋友可
💻 CPP
字号:
// ReadIP0802Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ReadIP0802.h"
#include "ReadIP0802Dlg.h"

//#include "UdpDialog.h"
//#include "TcpDialog.h"
//#include "icmpDlg.h"

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

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@




////////////////////////////////////////////////////
// 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()

//////////////////////////////////////////////////////////////////
// CReadIP0802Dlg dialog

CReadIP0802Dlg::CReadIP0802Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CReadIP0802Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReadIP0802Dlg)
	m_directory = _T("");
	m_version = _T("");
	m_hlen = _T("");
	m_upperprotocol = _T("");
	m_totallen = _T("");
	m_sourceip = _T("");
	m_destip = _T("");
	m_flag = _T("");
	m_offset = _T("");
	m_identification = _T("");
	m_tof = _T("");
	m_ttl = _T("");
	m_ipchecksum = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReadIP0802Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReadIP0802Dlg)
	DDX_Text(pDX, IDC_edit_directory, m_directory);
	DDX_Text(pDX, IDC_edit_version, m_version);
	DDX_Text(pDX, IDC_edit_hlen, m_hlen);
	DDX_Text(pDX, IDC_edit_upperprotocol, m_upperprotocol);
	DDX_Text(pDX, IDC_edit_totallen, m_totallen);
	DDX_Text(pDX, IDC_edit_sourceip, m_sourceip);
	DDX_Text(pDX, IDC_edit_destip, m_destip);
	DDX_Text(pDX, IDC_edit_flag, m_flag);
	DDX_Text(pDX, IDC_edit_fragoffset, m_offset);
	DDX_Text(pDX, IDC_edit_identification, m_identification);
	DDX_Text(pDX, IDC_edit_tos, m_tof);
	DDX_Text(pDX, IDC_edit_ttl, m_ttl);
	DDX_Text(pDX, IDC_edit_checksum, m_ipchecksum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReadIP0802Dlg, CDialog)
	//{{AFX_MSG_MAP(CReadIP0802Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BN_directory, OnBNdirectory)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////
// CReadIP0802Dlg message handlers

BOOL CReadIP0802Dlg::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
	bTcpopen=false;
	bUdpopen=false;
	bIcmpopen=false;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CReadIP0802Dlg::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 CReadIP0802Dlg::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 CReadIP0802Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CReadIP0802Dlg::OnBNdirectory() 
{
	// TODO: Add your control notification handler code here
	CFileDialog filedlg(TRUE,NULL,NULL,0,
		"IP包文件(*.dat)|*.dat|所有文件(*.*)|*.*||",NULL);
	if(filedlg.DoModal()==IDOK)
	{SetDlgItemText(IDC_edit_directory,filedlg.GetPathName());
	UpdateData(true);
	if(bTcpopen)	
		{tcpdlg.DestroyWindow();
		bTcpopen=false;}
	if(bUdpopen)
		{udpdlg.DestroyWindow();
		bUdpopen=false;}
	if(bIcmpopen)
		{
		idlg.DestroyWindow();
		bIcmpopen=false;
		}
//	}//@@@@@@@@
	UpdateData(true);
	file.Open(m_directory,CFile::modeRead);


	file.Read(&iphead,sizeof(iphead));
	file.Seek((iphead.verlen&0x0f)*4,CFile::begin);//确定下一头部的begin.
	//为各变量赋值:	
	m_version.Format(("IPv%x"),iphead.verlen>>4);
	m_hlen.Format(("%x(4Byte)"),iphead.verlen&0xf);
	m_tof.Format(("0x%x"),iphead.tof);
	m_totallen.Format(("0d%d/0x%x"),(iphead.totallenh
		<<8)+iphead.totallenl,(iphead.totallenh
		<<8)+iphead.totallenl);
	struct in_addr in1;			//
		in1.s_addr=iphead.sourceIP;	//
		m_sourceip=inet_ntoa(in1);		//
	struct in_addr in;			//
		in.s_addr=iphead.destIP;	//
		m_destip=inet_ntoa(in);		//
	m_identification.Format(("0x%x"),(iphead.identificationh<<8)+iphead.identificationl);
	m_flag.Format(("0x%x"),iphead.flagoffseth>>5);
	m_offset.Format(("0x%x"),iphead.flagoffseth&0x1f+iphead.offsetl);
	m_ttl.Format(("0x%x"),iphead.ttl);
	m_ipchecksum.Format(("0x%x"),(iphead.checksumh<<8)+iphead.checksuml);
			//用UPPER()函数使字符变大写如何做??

	switch(iphead.upperprotocol)
	{
	case 1:
		m_upperprotocol="ICMP";
		CReadIP0802Dlg::icmp();
		break;
	case 2:
		m_upperprotocol="IGMP";
		break;
	case 6:
		m_upperprotocol="TCP";
		CReadIP0802Dlg::tcp();
		break;
	case 17:
		m_upperprotocol="UDP";
		CReadIP0802Dlg::udp();
		break;
	default:
		m_upperprotocol="Other";
		break;
	}
	UpdateData(FALSE);
	file.Close();
	}//@@@@@@@@
}



void CReadIP0802Dlg::tcp()
{
	file.Read(&tcphead,sizeof(tcphead));

	tcpdlg.m_tcpsourceport.Format(("%d"),(tcphead.sourceporth<<8)
		+tcphead.sourceportl);
	tcpdlg.m_tcpdestinationport.Format(("%d"),(tcphead.destporth<<8)
		+tcphead.destportl);
	tcpdlg.m_sequence.Format(("0x%x"),(tcphead.sequence1<<24)
		+(tcphead.sequence2<<16)+(tcphead.sequence3<<8)
		+tcphead.sequence4);
	tcpdlg.m_acknowledge.Format(("0x%x"),(tcphead.ack1<<24)
		+(tcphead.ack2<<16)+(tcphead.ack3<<8)
		+tcphead.ack4);
	tcpdlg.m_tcphlen.Format(("0x%x"),tcphead.tcphlen_h4>>4);
	tcpdlg.m_alertpointer.Format(("0x%x"),(tcphead.urgentpointerh<<8)+tcphead.urgentpointerl);
	tcpdlg.m_tcpchecksum.Format(("0x%x"),(tcphead.tcpchecksumh<<8)+tcphead.tcpchecksuml);
	tcpdlg.m_codebit.Format(("0x%x"),tcphead.codebit_l6&0x3f);
	tcpdlg.m_window.Format(("0x%x"),(tcphead.windowh<<8)+tcphead.windowl);

	tcpdlg.Create(IDD_TCPDIALOG,NULL);
	tcpdlg.ShowWindow(SW_SHOW);
	bTcpopen=true;

//return();
}

void CReadIP0802Dlg::udp()
{

	file.Read(&udphead,sizeof(udphead));

	udpdlg.m_udpsourceport.Format(("0d%d"),(udphead.sourceporth<<8)+udphead.sourceportl);
	udpdlg.m_udpdestport.Format(("0d%d"),(udphead.destporth<<8)+udphead.destportl);
	udpdlg.m_datagramlen.Format(("0x%x"),(udphead.datagramlenh<<8)+udphead.datagramlenl);
	udpdlg.m_udpchecksum.Format(("0x%x"),(udphead.udpchecksumh<<8)+udphead.udpchecksuml);
/**/
	udpdlg.Create(IDD_UDPDIALOG,NULL);
	udpdlg.ShowWindow(SW_SHOW);
	bUdpopen=true;
}



void CReadIP0802Dlg::icmp()
{
	

	file.Read(&IcmpHead,sizeof(IcmpHead));

	idlg.m_icmptype.Format("%d",IcmpHead.i_type);
	idlg.m_icmpcode.Format("0x%x",IcmpHead.i_code);
	idlg.m_icmpchksum.Format("0x%x",IcmpHead.i_cksum);
	idlg.m_icmpsqu.Format("0x%x",IcmpHead.i_seq);
	idlg.m_icmpid.Format("0x%x",IcmpHead.i_id);
	
	idlg.Create(IDD_ICMPDLG,NULL);
	idlg.ShowWindow(SW_SHOW);
	bIcmpopen=true;
}/**/

⌨️ 快捷键说明

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