www.pudn.com > zhuabao.rar > PacketReplayDoc.h


// PacketReplayDoc.h : interface of the CPacketReplayDoc class 
// 
///////////////////////////////////////////////////////////////////////////// 
 
#if !defined(AFX_PACKETREPLAYDOC_H__F054C2EF_1D5C_4A9C_A60E_2786365BCDD2__INCLUDED_) 
#define AFX_PACKETREPLAYDOC_H__F054C2EF_1D5C_4A9C_A60E_2786365BCDD2__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
 
#include "pcap.h" 
#include "Set.h" 
#include "Send.h" 
 
//------------------------------------------------------------------------------------- 
#define TCP_PROTO 6  
#define UDP_PROTO 17  
#define ICMP_PROTO 1 
#define UDP_HEAD_LEN 8	 
#define ARP_HEAD_LEN 14 
#define ICMP_HEAD_LEN 4 
/***********一些说明********************************** 
//1字节=8位 
sizeof(u_char)==1 
sizeof(u_short)==2 
sizeof(u_int)==4 
sizeof(u_long)==4 
 
sizeof(u_int8_t)==1 
sizeof(u_int16_t)==2 
sizeof(u_int32_t)==4 
sizeof(u_int64_t)==8 
***********************************************/ 
//Mac帧头 占14个字节 
typedef struct tag_mac_header 
{ 
	u_char dadd[6];//6个字节 目标地址 
	u_char sadd[6];//6个字节 源地址 
	u_short mac_type;//2个字节 类型 
}mac_header; 
 
//IP地址 4字节 
typedef struct tag_ip_address 
{ 
	u_char byte1; //sizeof(u_char)==1  u_char占一个字节(8位) 
	u_char byte2; 
	u_char byte3; 
	u_char byte4; 
}ip_address; 
 
//定义IP头 IP数据包=IP头+TCP数据段(或UDP数据段) 
typedef struct tag_ip_header 
{ 
	u_char ver_ihl; // u_char 8位 版本(4位) + 头长(4位) 
	u_char tos;//TOS 服务类型 
	u_short tlen;//包总长 u_short占两个字节 
	u_short identification;//标识 
	u_short flags_fo;//标志位Flags(3位)+分段偏移(13位) 
	u_char ttl;//TTL 
	u_char proto;//Protocol 
	u_short crc;//校验和 
	ip_address saddr;//源地址 
	ip_address daddr;//目的地址 
	u_int op_pad;//选项+填充字段 u_int占4个字节 32位 
	//接下来是数据段,不属于IP头部 
}ip_header; 
 
//定义UDP头 
typedef struct tag_udp_header 
{ 
	u_short sport;//源端口  16位 
	u_short dport;//目的端口 16位 
	u_short len;//数据报长度 16位 
	u_short crc;//校验和 16位 
	 
}udp_header; 
 
//定义TCP头 
typedef struct tag_tcp_header 
{ 
	u_short sport;//源端口地址  16位 
	u_short dport;//目的端口地址 16位 
	u_int32_t sn;//序列号 32位 
	u_int32_t an;//确认号 32位 
	u_int16_t other;//16位 其中 头长度4位+保留6位+URG1+ACK1+PSH1+RST1+SYN1+FIN1  
	u_int16_t window_size;//窗口大小 16位 
	u_int16_t check_sum;//校验和 16位 
	u_int16_t urgent_pointer;//紧急指针 16位 
	u_int32_t option;//选项0或32位 
}tcp_header; 
 
//定义ICMP 
typedef struct tag_icmp_header 
{ 
	u_char type;//8位 类型 
	u_char identifer; //8位 代码 
	u_char sequence;//序列号 8位 
	u_char chksum;//8位校验和 
}icmp_header; 
////////////////////////////////////////////////////////////////////////// 
//要保存的数据结构 
typedef struct tag_DataPacket 
{ 
	char strPacketType[8];//包类型 
	int len;//长度 
	mac_header mach;//MAC头 
	ip_header iph;//IP头	 
	void* pTCP_UDP;//可能位tcp也可能位udp 无法预料,所以用指针保存 
	u_char *data; 
}DataPacket; 
 
//------------------------------------------------------------------------------------- 
 
 
class CPacketReplayDoc : public CDocument 
{ 
protected: // create from serialization only 
	CPacketReplayDoc(); 
	DECLARE_DYNCREATE(CPacketReplayDoc) 
 
// Attributes 
public: 
	HANDLE m_ThreadHandle; 
	CPtrList m_data; 
	int m_nPacketNum; //捕获到的数据包个数 
	u_int netmask;//掩码 
	int m_nDeviceCount; //设备个数 
	pcap_t *adhandle;//打开的网络设备的句并 
	pcap_if_t *alldevs; //所有设备列表 
	pcap_if_t *d; //某个设备 
	char errbuf[PCAP_ERRBUF_SIZE]; //保存错误信息 
	////////////////////////////////////////////////////////////////////////// 
	char m_strIndex[8]; //序号 
	char m_strProto[8]; //协议 
	char m_strSourIP[16];//源IP 
	char m_strDestIP[16];//目的IP 
	char m_strSourPort[10];//源端口 
	char m_strDestPort[10];//目的端口 
 
	char m_strSize[8];//包大小 
	char m_strID[8];//标识位 
	//char m_strTTL[8];//TTL 
	char m_strSN[10];//序列号 
	char m_strAN[10];//确认序列号 
	char m_strDataOff[10];//数据偏移,tcp头长 
 
	//char m_strURG[4];// 
	//char m_strACK[4];// 
	//char m_strPSH[4];// 
	//char m_strRST[4];// 
	//char m_strSYN[4];// 
	//char m_strFIN[4];// 
	//char m_strWindow[10];//窗口 
	//char m_strUrgPoint[10];//紧急指针 
	char m_strData[3000];//数据,从MAC地址开始 
	char m_strIPLen[10];//IP头长 
 
// Operations 
public: 
// Overrides 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CPacketReplayDoc) 
	public: 
	virtual BOOL OnNewDocument(); 
	virtual void Serialize(CArchive& ar); 
	//}}AFX_VIRTUAL 
 
// Implementation 
public: 
 
	int InitPacp(); 
	int StartCapture(int index); 
	int SavePacket(struct pcap_pkthdr *header,const u_char *pkt_data); 
	void CleanData(); 
	int AnalyzeData(DataPacket* pdata); 
	void UpdateTree(int index); 
	void UpdateEdit(int index); 
	int StopCapture(); 
 
	virtual ~CPacketReplayDoc(); 
#ifdef _DEBUG 
	virtual void AssertValid() const; 
	virtual void Dump(CDumpContext& dc) const; 
#endif 
 
protected: 
       
// Generated message map functions 
protected: 
	//{{AFX_MSG(CPacketReplayDoc) 
	afx_msg void OnSet(); 
	afx_msg void OnReplay(); 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
private: 
	CSet set; 
	CSend  *psend; 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
 
//{{AFX_INSERT_LOCATION}} 
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. 
 
#endif // !defined(AFX_PACKETREPLAYDOC_H__F054C2EF_1D5C_4A9C_A60E_2786365BCDD2__INCLUDED_)