--------------------------------------------------------
|48bit 目的物理地址 | 48bit 源物理地址 | 16bit 协议地址|
--------------------------------------------------------
相应的数据结构如下
struct ethhdr
{
unsigned char h_dest[ETH_ALEN];
unsigned char h_source[ETH_ALEN];
unsigned short h_proto;
}
其中h_dest[6]是48位的目标地址的网卡物理地址,h_source [6] 是48位的源地址的物理网卡地址。H_proto是16位的以太网协议,其中主要有0x0800 ip,0x8035.X25,0x8137 ipx,0x8863-0x8864 pppoe(这是Linux的 ppp),0x0600 ether _loop_back ,0x0200-0x0201 pup等。Iphdr 这是ip协议的报头:
由此可以定义其结构如下:
struct iphdr
{
#elif defined (_LITTLE_ENDIAN_BITFIELD)
_u8 version :4,
#elif defined (_BIG_ENDIAN_BITFIELD)
_u8 version:4,
ihl:4;
#else
#error "Please fix"
#endif
_u8 tos;
_16 tot_len;
_u16 id;
_u16 frag_off;
_u8 ttl;
_u8 protocol;
_u16 check;
_u32 saddr;
_u32 daddr;
};
这是Linux 的ip协议报头,针对版本的不同它可以有不同的定义,我们国内一般用BIG的定义,其中version 是ip的版本,protocol是ip的协议分类主要有0x06 tcp.0x11 udp,0x01 icmp,0x02 igmp等,saddr是32位的源ip地址,daddr是32位的目标ip地址。
相应的数据结构:
struct arphdr
{
unsigned short int ar_hrd;
unsigned short int ar_pro;
unsigned char ar_hln;
unsigned char ar_pln;
unsigned short int ar_op;
#if 0
unsigned char _ar_sha[ETH_ALEN];
unsigned char _ar_sip[4];
unsigned char _ar_tha[ETH_ALEN];
unsigned char _ar_tip[4];
#end if
};
这是linux 的arp 协议报头,其中ar_hrd 是硬件地址的格式,ar_pro协议地址的格式,ar_hln是硬件地址的长度,ar_pln时协议地址的长度,ar_op是arp协议的分类0x001是arp echo 0x0002 是 arp reply.接下来的分别是源地址的物理地址,源ip地址,目标地址的物理地址,目标ip地址。
Tcphdr ip协议的tcp协议报头
以下是相应数据结构:
struct tcphdr
{
u_int16_t source;
u_int16_t dest;
u_int32_t seq;
u_int32_t ack_seq;
# if _BYTE_ORDER == _LITTLE _ENDIAN
u_int16_t resl:4;
u_int16_t doff:4;
u_int16_t fin:1;
u_int16_t syn:1;
u_int16_t rst:1;
u_int16_t psh:1;
u_int16_t ack:1;
u_int16_t urg:1;
u_int16_t res2:2;
#elif _BYTE _ORDER == _BIG _ENDIAN
u_int16_t doff:4;
u
_int16_t res1:4;
u_int16_t res2:2;
u_int16_t urg:1;
u_int16_t ack:1;
u_int16_t psh:1;
u_int16_t rst:1;
u_int16_t syn:1;
u_int16_t fin:1;
#else
#error "Adjust your defines"
#endif
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
};
这是Linux 下tcp协议的一部分与ip协议相同取BIG,其中source是源端口,dest 是目的端口,seq是s序,ack_seq是a序号,其余的是tcp的连接标志其中包括6个标志:syn表示连接请求,urg 表示紧急信息,fin表示连接结束,ack表示连接应答,psh表示推栈标志,rst表示中断连接。window是表示接受数据窗口大小,check是校验码,urg ptr是紧急指针。
Udphdr 这是udp协议报头
struct udphdr {
u_int16_t source;
u_int16_t dest;
u_int16_t len;
u_int16_t check;
}
