您的位置:首页 > 编程语言 > C语言/C++

报文解析各协议结构体

2013-01-15 08:59 344 查看
#pragma once

#include <WinSock2.h>

#pragma pack(1)

typedef struct tagEthernetIIPacketTail
{
UCHAR chDesAddr[6];
UCHAR chSrcAddr[6];
USHORT type;
}tagEthernetIIPacketTail;

#pragma pack()

#pragma pack(1)
typedef struct CDataHead
{
struct timeval ts;
UINT32 caplen;     /* length of portion present */
UINT32 len;        /* length this packet (off wire) */
}CDataHeader;

struct CDataHeadAndEthernet
{
CDataHead header;
tagEthernetIIPacketTail ethernet;

#define header_ts header.ts
#define header_caplen header.caplen
#define header_len header.len
};
#pragma pack()

#pragma pack(1)

struct iphdr {
UCHAR ihl:4,
version:4;
UCHAR	tos;
USHORT	tot_len;
USHORT	id;
union{
USHORT frag_off;

struct ip_flag
{
UCHAR flag_unused:5,
flag_MoreFrag:1,
flag_DontFrag:1,
flag_Reserve:1;
UCHAR reserve;
}u_flag;
}u_flag_off;

UCHAR	ttl;
UCHAR	protocol;
USHORT	check;
UINT32	saddr;
UINT32	daddr;
/*The options start here. */
};
#pragma pack()
#pragma pack(1)
typedef struct arphdr
{
//arp首部
unsigned short arp_hrd;    /* format of hardware address */
unsigned short arp_pro;    /* format of protocol address */
unsigned char arp_hln;    /* length of hardware address */
unsigned char arp_pln;    /* length of protocol address */
unsigned short arp_op;     /* ARP/RARP operation */

unsigned char arp_sha[6];    /* sender hardware address */
unsigned long arp_spa;    /* sender protocol address */
unsigned char arp_tha[6];    /* target hardware address */
unsigned long arp_tpa;    /* target protocol address */
}ARPHDR, *PARPHDR;
#pragma pack()
#pragma pack(1)
typedef struct DhcpPacketPart
{
UCHAR op;   // 反向为2
UCHAR hType;   // 硬件类别,ethernet为1
UCHAR hLen;    // 硬件长度,ethernet为6
UCHAR hops;    // 若数据包需经过router传送,每站加1,若在同一网内,为0
UINT32 transactionID;    // 事务ID,是个随机数,用于客户和服务器之间匹配请求和相应消息
USHORT seconds;    // 由用户指定的时间,指开始地址获取和更新进行后的时间
USHORT Flags;    // 从0-15bits,最左一bit为1时表示server将以广播方式传送封包给 client,其余尚未使用;
UINT32 ciAddr;    // 用户IP地址;
UINT32 yiAddr;    // 客户IP地址;
UINT32 siAddr;    // 用于bootstrap过程中的IP地址;
UINT32 giAddr;    // 转发代理(网关)IP地址;
char chaddr[16];    // :client的硬件地址;
char sname[64];   // :可选server的名称,以0x00结尾;
char File[128];   // 启动文件名;
//? Options没有解析,有无表示该选项存在的标志
}DhcpPacketPart;
#pragma pack()
#pragma pack(1)
typedef struct option
{
UCHAR type;
UCHAR Len;
CString value;
}option;
#pragma pack()
#pragma pack(1)
typedef struct DnsPacketPart
{
unsigned short u16Identification;
unsigned char rd:1
,tc:1
,aa:1
,Opcode:4
,qa:1;
unsigned char rcod:4
,cd:1
,ad:1
,z:1
,ra:1;
unsigned short u16Questions;
unsigned short u16AnswerRRs;
unsigned short u16AuthorityRRs;
unsigned short u16AdditionalRRs;
}DnsPacketPart;
#pragma pack()
#pragma pack(1)
typedef struct tagQuestionInfo
{
CString wsDomainName;
USHORT u16Type;
USHORT u16Class;
}tagQuestionInfo;
#pragma pack()
#pragma pack(1)
typedef struct tagResourceRecordInfo
{
CString wsDomainName;
USHORT u16Type;
USHORT u16Class;
UINT32 u32TTL;
USHORT u16RdataLen;
CString wsRdata;
}tagAnswerInfo;
#pragma pack()
#pragma pack(1)
typedef struct GtpPacketHeader
{
UCHAR
pn:1
,s:1
,e:1
,x:1
,pt:1
,ver:3;
UCHAR messageType;
USHORT hLen;
UINT32 teId;
USHORT sequenceNumber;
UCHAR npduNumber;
UCHAR nextExtensionHeaderType;
}GtpPacketHeader;
#pragma pack()
#pragma pack(1)
struct icmp6hdr {

UCHAR		icmp6_type;
UCHAR		icmp6_code;
USHORT		icmp6_cksum;

union {
UINT32			un_data32[1];
USHORT			un_data16[2];
UCHAR			un_data8[4];

struct icmpv6_echo {
USHORT		identifier;
USHORT		sequence;
} u_echo;

struct icmpv6_nd_advt {
UINT32		router:1,
solicited:1,
override:1,
reserved:29;
} u_nd_advt;

struct icmpv6_nd_ra {
UCHAR		hop_limit;
UCHAR		managed:1,
other:1,
router_pref:2,
reserved:4;
USHORT		rt_lifetime;
} u_nd_ra;

} icmp6_dataun;

#define icmp6_identifier	icmp6_dataun.u_echo.identifier
#define icmp6_sequence		icmp6_dataun.u_echo.sequence
#define icmp6_pointer		icmp6_dataun.un_data32[0]
#define icmp6_mtu		icmp6_dataun.un_data32[0]
#define icmp6_unused		icmp6_dataun.un_data32[0]
#define icmp6_maxdelay		icmp6_dataun.un_data16[0]
#define icmp6_nextHopMTU	icmp6_dataun.un_data16[1]
#define icmp6_router		icmp6_dataun.u_nd_advt.router
#define icmp6_solicited		icmp6_dataun.u_nd_advt.solicited
#define icmp6_override		icmp6_dataun.u_nd_advt.override
#define icmp6_ndiscreserved	icmp6_dataun.u_nd_advt.reserved
#define icmp6_hop_limit		icmp6_dataun.u_nd_ra.hop_limit
#define icmp6_addrconf_managed	icmp6_dataun.u_nd_ra.managed
#define icmp6_addrconf_other	icmp6_dataun.u_nd_ra.other
#define icmp6_rt_lifetime	icmp6_dataun.u_nd_ra.rt_lifetime
#define icmp6_router_pref	icmp6_dataun.u_nd_ra.router_pref
};
#pragma pack()
#pragma pack(1)
typedef struct RtpPacketPart
{
USHORT
pt:7
,m:1
,cc:4
,x:1
,p:1
,ver:2;
USHORT sequenceNumber;
UINT32 timestamp;
UINT32 ssrc;
//? CSRC没有解析,有无表示该选项存在的标志
}RtpPacketPart;
#pragma pack()
#pragma pack(1)
typedef struct StpPacketPart
{
USHORT protocolIndentifier;
UCHAR ver;
UCHAR messageType;
UCHAR flages;
USHORT rootPriority;
UCHAR rootId[6];
UINT32 rotPathCost;
USHORT bridgePriority;
UCHAR	bridgeId[6];
USHORT portId;
USHORT messageAge;
USHORT maximum;
USHORT helloTime;
USHORT forwardDelay;
}StpPacketPart;
#pragma pack()
#pragma pack(1)
typedef struct tcphdr {
USHORT	source;
USHORT	dest;
UINT32	seq;
UINT32	ack_seq;
UCHAR	res1:4,
doff:4;
UCHAR	fin:1,
syn:1,
rst:1,
psh:1,
ack:1,
urg:1,
ece:1,
cwr:1;
USHORT	window;
USHORT	check;
USHORT	urg_ptr;
}TCPHDR,*PTCPHDR;
#pragma pack()
#pragma pack(1)
typedef struct tcpoption
{
CString value;
}TCPOPTION;
#pragma pack()
#pragma pack(1)
typedef struct udphdr {
USHORT  source;
USHORT  dest;
USHORT  len;
USHORT check;
}UDPHDR, *PUDPHDR;
#pragma pack()
#pragma pack(1)
typedef struct VrrpPacketPart
{
unsigned char u8VerAndType;
unsigned char u8VirtualRtrID;
unsigned char u8Priority;
unsigned char u8CountIPAddrs;
unsigned char u8AuthType;
unsigned char u8AdverInt;
USHORT	u16Checksum;
}VrrpPacketPart;

#pragma pack()

#pragma pack(1)

typedef union {
unsigned char b[4];
UINT32 l;
} IPaddr;

#pragma pack()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息