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

📄 sinpackview.cpp

📁 这是一个嗅探器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SinPackView.cpp : implementation file
//

#include "stdafx.h"
#include "snifferpro.h"
#include "SinPackView.h"
#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSinPackView

IMPLEMENT_DYNCREATE(CSinPackView, CTreeView)

CSinPackView::CSinPackView()
{
	ip=NULL;
	ether=NULL;
	tcp=NULL;
	udp=NULL;
	icmp=NULL;
	arp=NULL;
	rarp=NULL;
}

CSinPackView::~CSinPackView()
{
}


BEGIN_MESSAGE_MAP(CSinPackView, CTreeView)
	//{{AFX_MSG_MAP(CSinPackView)
	ON_MESSAGE(WM_MESSAGE_PACKET_SELECT,OnPacketSelect)
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	ON_NOTIFY_REFLECT(TVN_SELCHANGING, OnSelchanging)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSinPackView drawing

void CSinPackView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CSinPackView diagnostics

#ifdef _DEBUG
void CSinPackView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CSinPackView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSinPackView message handlers

void CSinPackView::OnInitialUpdate() 
{
	CTreeView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	((CMainFrame *)AfxGetApp()->GetMainWnd())->sinPackView=this;
	CTreeCtrl &ctrl=this->GetTreeCtrl();
	DWORD m_dwstyle = ::GetWindowLong(ctrl.m_hWnd,GWL_STYLE);
	m_dwstyle |= TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT;
	::SetWindowLong(ctrl.m_hWnd,GWL_STYLE,m_dwstyle);
}

void CSinPackView::OnPacketSelect(const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
{
	//清空上次残留信息
	if(ip!=NULL){
		delete ip;
		ip=NULL;
	}
	if(ether!=NULL){
		delete ether;
		ether=NULL;
	}
	if(tcp!=NULL){
		delete tcp;
		tcp=NULL;
	}
	if(udp!=NULL){
		delete udp;
		udp=NULL;
	}
	if(icmp!=NULL){
		delete icmp;
		icmp=NULL;
	}
	if(arp!=NULL){
		delete arp;
		arp=NULL;
	}
	if(rarp!=NULL){
		delete rarp;
		rarp=NULL;
	}

	CTreeCtrl &ctrl=this->GetTreeCtrl();
	ctrl.DeleteAllItems();

	
	//Init for Ether 解析以太帧
	ether=new EtherHead(pkt_data,14);	
	CString strmactype;
	strmactype.Format("%d",ether->type);
	HTREEITEM macdest=ctrl.InsertItem("Mac Destination Address:  "+ether->GetMacDestAddr(),0);
	HTREEITEM macsrc=ctrl.InsertItem("Mac Source Address:  "+ether->GetMacSrcAddr(),0);
	HTREEITEM mactype=ctrl.InsertItem("Mac Frame Type: "+strmactype+"  "+ether->GetType(),0);
	HTREEITEM macdata=ctrl.InsertItem("Mac Data:",0);

	//Init for IP 
	if(ether->type==0x0800){
		ip=new IPGram(pkt_data+14,pkt_header->len);
		CString stripversion;
		CString stripIHL;
		CString stripprecedence;
		CString stripdelay;
		CString stripthroughtput;
		CString stripreliability;
		CString stripservicetype;
		CString striptotallen;
		CString stripid;
		CString stripDF;
		CString stripMF;
		CString stripfragoffset;
		CString stripTTL;
		CString stripproto;
		CString stripchecksum;
		CString stripdatalen;
		CString stripopt;
		stripversion.Format("%d",ip->version);
		stripIHL.Format("%d",ip->IHL*4);
		stripservicetype.Format("%d",ip->servicetype);
		stripprecedence.Format("%d",ip->precedence);
		striptotallen.Format("%d",ip->totallen);
		stripid.Format("%d",ip->identification);
		stripopt.Format("%d",ip->optlen);
		if (ip->DF)
			stripDF.Format("True");
		else
			stripDF.Format("False");
		if (ip->MF)
			stripMF.Format("True");
		else
			stripMF.Format("False");
		if (ip->delay)
			stripdelay.Format("True");
		else
			stripdelay.Format("False");
		if (ip->throughtput)
			stripthroughtput.Format("True");
		else
			stripthroughtput.Format("False");
		if (ip->reliability)
			stripreliability.Format("True");
		else
			stripreliability.Format("False");
		stripfragoffset.Format("%d",ip->fragoffset);
		stripTTL.Format("%d",ip->TTL);
		stripproto.Format("%d",ip->protocol);
		stripchecksum.Format("0x%X",ip->checksum);
		stripdatalen.Format("%d",ip->datalen);
		HTREEITEM ipverion=ctrl.InsertItem("IP Version:  "+stripversion,macdata);
		HTREEITEM ipIHL=ctrl.InsertItem("Length of IP Head :  "+stripIHL+" Byte",macdata);
		HTREEITEM ipservetype=ctrl.InsertItem("Service Type:  "+stripservicetype,macdata);
		HTREEITEM ipprecedence=ctrl.InsertItem("Precedence:  "+stripprecedence,macdata);
		HTREEITEM ipdelay=ctrl.InsertItem("Delay:  "+stripdelay,ipprecedence);
		HTREEITEM ipthroughtput=ctrl.InsertItem("Throughtput:  "+stripthroughtput,ipprecedence);
		HTREEITEM ipreliability=ctrl.InsertItem("Reliability:  "+stripreliability,ipprecedence);
		HTREEITEM iptotallen=ctrl.InsertItem("Total Length:  "+striptotallen+" Byte",macdata);
		HTREEITEM ipid=ctrl.InsertItem("IP Identification:  "+stripid,macdata);
		HTREEITEM ipDF=ctrl.InsertItem("DF:  "+stripDF,macdata);
		HTREEITEM ipMF=ctrl.InsertItem("MF:  "+stripMF,macdata);
		HTREEITEM ipfragoffset=ctrl.InsertItem("Fragment Offset:  "+stripfragoffset,macdata);
		HTREEITEM ipTTL=ctrl.InsertItem("Time to Live:  "+stripTTL+" Second",macdata);
		HTREEITEM ipproto=ctrl.InsertItem("IP Protocol: "+stripproto+"  "+ip->GetService(),macdata);
		HTREEITEM ipchecksum=ctrl.InsertItem("IP Header Check Sum:  "+stripchecksum,macdata);
		HTREEITEM ipsrc=ctrl.InsertItem("IP Source Address:  "+ip->GetSrcAddr(),macdata);
		HTREEITEM ipdest=ctrl.InsertItem("IP Destination Address:  "+ip->GetDestAddr(),macdata);
		HTREEITEM ipopt=ctrl.InsertItem("IP Options:  "+stripopt+" Byte",macdata);
		HTREEITEM ipdata=ctrl.InsertItem("IP Data:  "+stripdatalen+" Byte",macdata);
	
		if(ip->protocol==6){//TCP
			tcp=new TCPGram(ip->data,ip->datalen);
			CString strtcpsrcport;
			CString strtcpdestport;
			CString strtcpseq;
			CString strtcpack;
			CString strtcpheadlen;
			CString strtcpURG;
			CString strtcpACK;
			CString strtcpPSH;
			CString strtcpRST;
			CString strtcpSYN;
			CString strtcpFIN;
			CString strtcpwinsize;
			CString strtcpchecksum;
			CString strtcpurgpos;
			CString strtcpdatalen;
			CString strtcpopt;
			strtcpsrcport.Format("%d",tcp->srcport);
			strtcpdestport.Format("%d",tcp->destport);
			strtcpseq.Format("%u",tcp->seqnum);
			strtcpack.Format("%u",tcp->acknum);
			strtcpheadlen.Format("%d",tcp->headlen*4);
			if(tcp->URG)
				strtcpURG.Format("True");
			else
				strtcpURG.Format("False");
			if(tcp->ACK)
				strtcpACK.Format("True");
			else
				strtcpACK.Format("False");
			if(tcp->PSH)
				strtcpPSH.Format("True");
			else
				strtcpPSH.Format("False");
			if(tcp->RST)
				strtcpRST.Format("True");
			else
				strtcpRST.Format("False");
			if(tcp->SYN)
				strtcpSYN.Format("True");
			else
				strtcpSYN.Format("False");
			if(tcp->FIN)
				strtcpFIN.Format("True");
			else
				strtcpFIN.Format("False");
			strtcpwinsize.Format("%d",tcp->windowsize);
			strtcpchecksum.Format("0x%X",tcp->checksum);
			strtcpurgpos.Format("%d",tcp->urgpos);
			strtcpdatalen.Format("%d",tcp->datalen);
			strtcpopt.Format("%d",tcp->optlen);
			HTREEITEM tcpsrc=ctrl.InsertItem("TCP Source Port:  "+strtcpsrcport,ipdata);
			HTREEITEM tcpdest=ctrl.InsertItem("TCP Destination Port:  "+strtcpdestport,ipdata);
			HTREEITEM tcpseq=ctrl.InsertItem("Sequence Num:  "+strtcpseq,ipdata);
			HTREEITEM tcpack=ctrl.InsertItem("Acknowledgement Num:  "+strtcpack,ipdata);
			HTREEITEM tcpheadlen=ctrl.InsertItem("Length of TCP Head:  "+strtcpheadlen+" Byte",ipdata);
			HTREEITEM tcpflag=ctrl.InsertItem("TCP Flags:",ipdata);
			HTREEITEM tcpURG=ctrl.InsertItem("URG:  "+strtcpURG,tcpflag);
			HTREEITEM tcpACK=ctrl.InsertItem("ACK:  "+strtcpACK,tcpflag);
			HTREEITEM tcpPSH=ctrl.InsertItem("PSH:  "+strtcpPSH,tcpflag);
			HTREEITEM tcpRST=ctrl.InsertItem("RST:  "+strtcpRST,tcpflag);
			HTREEITEM tcpSYN=ctrl.InsertItem("SYN:  "+strtcpSYN,tcpflag);
			HTREEITEM tcpFIN=ctrl.InsertItem("FIN:  "+strtcpFIN,tcpflag);
			HTREEITEM tcpwinsize=ctrl.InsertItem("Window Size:  "+strtcpwinsize+" Byte",ipdata);
			HTREEITEM tcpchecksum=ctrl.InsertItem("TCP Header Check Sum:  "+strtcpchecksum,ipdata);
			HTREEITEM tcpurgpos=ctrl.InsertItem("Urgent Pointer:  "+strtcpurgpos,ipdata);
			HTREEITEM tcpopt=ctrl.InsertItem("TCP Options:  "+strtcpopt+" Byte",ipdata);
			HTREEITEM tcpdata=ctrl.InsertItem("TCP Data:  "+strtcpdatalen+" Byte",ipdata);
			ctrl.Expand(tcpflag,TVE_EXPAND);
		}//Init for TCP
		
		else if(ip->protocol==17){//Init for UDP
			udp=new UDPGram(ip->data,ip->datalen);
			CString strudpsrcport;
			CString strudpdestport;
			CString strudplen;
			CString strudpchecksum;
			CString strudpdatalen;
			strudpsrcport.Format("%u",udp->srcport);
			strudpdestport.Format("%u",udp->destport);
			strudplen.Format("%u",udp->totallen);
			strudpchecksum.Format("%X",udp->checksum);
			strudpdatalen.Format("%u",udp->datalen);
			HTREEITEM udpsrcport=ctrl.InsertItem("UDP Source Port:  "+strudpsrcport,ipdata);
			HTREEITEM udpdestport=ctrl.InsertItem("UDP Destination Port:  "+strudpdestport,ipdata);
			HTREEITEM udplen=ctrl.InsertItem("UDP Length:  "+strudplen+" Byte",ipdata);
			HTREEITEM udpchecksum=ctrl.InsertItem("UDP Header Check Sum:  "+strudpchecksum,ipdata);
			HTREEITEM udpdata=ctrl.InsertItem("UDP Data:  "+strudpdatalen+" Byte",ipdata);
		}//Init for UDP

		else if(ip->protocol==1){//Init for ICMP
			icmp=new ICMPGram(ip->data,ip->datalen);
			CString stricmptype;
			CString stricmpcode;
			CString stricmpchecksum;
			CString stricmpdatalen;
			CString stricmptypevalue;
			CString stricmpcodevalue;
			CString stricmpid;
			CString stricmpseq;
			CString stricmpinittime;
			CString stricmpreceivetime;
			CString stricmpsendtime;

			icmp->GetType(stricmptype,stricmpcode);
			stricmpchecksum.Format("0x%X",icmp->checksum);
			stricmpdatalen.Format("%d",icmp->datalen);
			stricmptypevalue.Format("%d",icmp->type);
			stricmpcodevalue.Format("%d",icmp->code);
			HTREEITEM icmptype=ctrl.InsertItem("ICMP Type: "+stricmptypevalue+"  "+stricmptype,ipdata);
			HTREEITEM icmpcode=ctrl.InsertItem("ICMP Code: "+stricmpcodevalue+"  "+stricmpcode,ipdata);
			HTREEITEM icmpchecksum=ctrl.InsertItem("ICMP Check Sum:  "+stricmpchecksum,ipdata);
			switch(icmp->type){
			case 3:
				//stricmpid.Format("0");
				//stricmpseq.Format("0");
				break;
			case 5:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				ctrl.InsertItem("GateWay:  "+icmp->GetGateWay(),ipdata);
				break;
			case 8:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				break;
			case 0:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				break;
			case 13:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				stricmpinittime.Format("%u",icmp->inittime);
				stricmpreceivetime.Format("%u",icmp->receivetime);
				stricmpsendtime.Format("%d",icmp->sendtime);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				ctrl.InsertItem("Initial Time:  "+stricmpinittime,ipdata);
				ctrl.InsertItem("Receiving Time:  "+stricmpreceivetime,ipdata);
				ctrl.InsertItem("Sending Time:  "+stricmpsendtime,ipdata);
				break;
			case 14:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				stricmpinittime.Format("%u",icmp->inittime);
				stricmpreceivetime.Format("%u",icmp->receivetime);
				stricmpsendtime.Format("%d",icmp->sendtime);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				ctrl.InsertItem("Initial Time:  "+stricmpinittime,ipdata);
				ctrl.InsertItem("Receiving Time:  "+stricmpreceivetime,ipdata);
				ctrl.InsertItem("Sending Time:  "+stricmpsendtime,ipdata);
				break;
			case 17:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				ctrl.InsertItem("Address Mask:  "+icmp->GetAddrMask(),ipdata);
				break;
			case 18:
				stricmpid.Format("%u",icmp->identification);
				stricmpseq.Format("%u",icmp->sequence);
				ctrl.InsertItem("ICMP Identification:  "+stricmpid,ipdata); 
				ctrl.InsertItem("ICMP Sequence:  "+stricmpseq,ipdata);
				ctrl.InsertItem("Address Mask:  "+icmp->GetAddrMask(),ipdata);
				break;
			}
			HTREEITEM icmpdata=ctrl.InsertItem("ICMP Data:  "+stricmpdatalen+" Byte",ipdata);
		}//Init for ICMP
		ctrl.Expand(ipprecedence,TVE_EXPAND);
		ctrl.Expand(ipdata,TVE_EXPAND);
	}//Init for IP

	//Init for ARP 
	else if(ether->type==0x0806){
		arp=new ARPGram(pkt_data+14,pkt_header->len-14);
		CString strarphdwtype;
		CString strarpprttype;
		CString strarphdwaddrlen;
		CString strarpprtaddrlen;
		strarphdwtype.Format("%u",arp->hdwaddrtype);
		strarpprttype.Format("%u",arp->prtaddrtype);
		strarphdwaddrlen.Format("%u",arp->hdwaddrlen);
		strarpprtaddrlen.Format("%u",arp->prtaddrlen);
		HTREEITEM arphdwtype=ctrl.InsertItem("Hardware Address Type:  "+strarphdwtype,macdata);
		HTREEITEM arpprttype=ctrl.InsertItem("Protocol Address Type: "+strarpprttype+"  "+arp->GetPrtType(),macdata);

⌨️ 快捷键说明

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