IP协议族协议头结构(含 c声明 和 rfc 字符图示)
时间:2006-06-25 13:46:02 来源:本站搜集整理 作者:Eric
//IP协议族协议头结构(含 c声明 和 rfc 字符图示)
//Jurassic 2003.3.6 created.
/*
TCP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Source Port | Destination Port |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Sequence Number |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Acknowledgment Number |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Checksum | Urgent Pointer |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Options | Padding |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| data |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TCP Header Format
RFC-793
--*/
typedef
struct _tcphdr
{
unsigned short source; //原端口地址
unsigned short dest; //目的端口地址
unsigned long seq; //序号
unsigned long ack_seq; //确认号
unsigned short doff:4; //首部长度
unsigned short resl:4; //保留
unsigned short cwr:1; //控制
unsigned short ece:1;
unsigned short urg:1;
unsigned short ack:1;
unsigned short psh:1;
unsigned short rst:1;
unsigned short syn:1;
unsigned short fin:1;
unsigned short window;
unsigned short check;
unsigned short urg_ptr;
}tcphdr;
/*
IP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|Version| IHL |Type of Service| Total Length |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Identification |Flags| Fragment Offset |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Time to Live | Protocol | Header Checksum |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Source Address |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Destination Address |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Options | Padding |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IP Header Format
RFC-791
--*/
typedef
struct _iphdr
{
unsigned char version:4; //版本
unsigned char ihl:4; //首部长度
unsigned char tos; //服务类型
unsigned short tot_len; //总长度
unsigned short id; //标志
unsigned short frag_off; //分片偏移
unsigned char ttl; //生存时间
unsigned char protocol; //协议
unsigned char check; //检验和
unsigned long saddr; //源IP地址
unsigned long daaddr; //目的IP地址
}iphdr;
/*
UDP Header Format
0 7 8 15 16 23 24 31
-------- -------- -------- --------
| source address |
-------- -------- -------- --------
| destination address |
-------- -------- -------- --------
| zero |protocol| UDP length |
-------- -------- -------- --------
UDP Header Format
RFC-768
--*/
typedef
struct _udphdr
{
unsigned short source;
unsigned short dest;
unsigned short len;
unsigned short check;}udphdr;
/*
ICMP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Type | Code | Checksum |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| unused |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Internet Header 64 bits of Original Data Datagram |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ICMP Header Format
RFC-792
--*/
typedef
struct _icmphdr
{
unsigned char type; //类型
unsigned char code; //代码
unsigned short checksum; //校验和
union {
struct {
unsigned short id; //标识符
unsigned short sequence; //序号
} echo;
unsigned long gateway; //目标路由器的IP地址
struct {
unsigned short unused; //
unsigned short mtu;
} frag;
} un;
} icmphdr;
/*
IGMP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Type | Code | Checksum |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Identifier |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Group Address |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| |
Access Key
| |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IGMP Header FormatRFC-988
--*/
typedef
struct _igmphdr
{
unsigned char type;
unsigned char code; /* For newer IGMP */
unsigned short csum;
unsigned long group;
} igmphdr;
/*
IPv6 Header Format
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Ver. | IHL | TOS | Total Length |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Identification |Flags| Fragment Offset |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|1|1|1|1|1|0| Offset| Reserved | Source IP address part 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|1|1|1|1|1|0| Offset| Reserved | Destination IP address part 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: Options :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: SADDR Code |Len adr. part 2| Source IP address part 2 :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: DADDR Code |Len adr. part 2| Destination IP address part 2 :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: Data :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IPv6 Header Format
RFC-1365
--*/
typedef
struct _ipv6hdr
{
unsigned char version:4;
unsigned char priority:4;
unsigned char flow_lbl[3];
unsigned short payload_len;
unsigned char nexthdr;
unsigned char hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
} ipv6hdr;
/*
* This is an Ethernet frame header.
*/
typedef
struct _ethhdr
{
unsigned char h_dest[ETH_ALEN]; // destination eth addr
unsigned char h_source[ETH_ALEN]; // source ether addr
unsigned short h_proto; // packet type ID field
} ethhdr;
/* FDDI
Define 802.2 Type 1 header */
struct fddi_8022_1_hdr
{
__u8 dsap; /* destination service access point */
__u8 ssap; /* source service access point */
__u8 ctrl; /* control byte #1 */
} __attribute__ ((packed));
/* Define 802.2 Type 2 header */
struct fddi_8022_2_hdr
{
__u8 dsap; /* destination service access point */
__u8 ssap; /* source service access point */
__u8 ctrl_1; /* control byte #1 */
__u8 ctrl_2; /* control byte #2 */
} __attribute__ ((packed));
/* Define 802.2 SNAP header */
#define FDDI_K_OUI_LEN 3
struct fddi_snap_hdr
{
__u8 dsap; /* always 0xAA */
__u8 ssap; /* always 0xAA */
__u8 ctrl; /* always 0x03 */
__u8 oui[FDDI_K_OUI_LEN]; /* organizational universal id */
__u16 ethertype; /* packet type ID field */
} __attribute__ ((packed));
/* Define FDDI LLC frame header */
struct fddihdr
{
__u8 fc; /* frame control */
__u8 daddr[FDDI_K_ALEN]; /* destination address */
__u8 saddr[FDDI_K_ALEN]; /* source address */
union
{
struct fddi_8022_1_hdr llc_8022_1;
struct fddi_8022_2_hdr llc_8022_2;
struct fddi_snap_hdr llc_snap;
} hdr;
} __attribute__ ((packed));
//Jurassic 2003.3.6 created.
/*
TCP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Source Port | Destination Port |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Sequence Number |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Acknowledgment Number |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Checksum | Urgent Pointer |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Options | Padding |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| data |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TCP Header Format
RFC-793
--*/
typedef
struct _tcphdr
{
unsigned short source; //原端口地址
unsigned short dest; //目的端口地址
unsigned long seq; //序号
unsigned long ack_seq; //确认号
unsigned short doff:4; //首部长度
unsigned short resl:4; //保留
unsigned short cwr:1; //控制
unsigned short ece:1;
unsigned short urg:1;
unsigned short ack:1;
unsigned short psh:1;
unsigned short rst:1;
unsigned short syn:1;
unsigned short fin:1;
unsigned short window;
unsigned short check;
unsigned short urg_ptr;
}tcphdr;
/*
IP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|Version| IHL |Type of Service| Total Length |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Identification |Flags| Fragment Offset |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Time to Live | Protocol | Header Checksum |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Source Address |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Destination Address |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Options | Padding |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IP Header Format
RFC-791
--*/
typedef
struct _iphdr
{
unsigned char version:4; //版本
unsigned char ihl:4; //首部长度
unsigned char tos; //服务类型
unsigned short tot_len; //总长度
unsigned short id; //标志
unsigned short frag_off; //分片偏移
unsigned char ttl; //生存时间
unsigned char protocol; //协议
unsigned char check; //检验和
unsigned long saddr; //源IP地址
unsigned long daaddr; //目的IP地址
}iphdr;
/*
UDP Header Format
0 7 8 15 16 23 24 31
-------- -------- -------- --------
| source address |
-------- -------- -------- --------
| destination address |
-------- -------- -------- --------
| zero |protocol| UDP length |
-------- -------- -------- --------
UDP Header Format
RFC-768
--*/
typedef
struct _udphdr
{
unsigned short source;
unsigned short dest;
unsigned short len;
unsigned short check;}udphdr;
/*
ICMP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Type | Code | Checksum |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| unused |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Internet Header 64 bits of Original Data Datagram |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ICMP Header Format
RFC-792
--*/
typedef
struct _icmphdr
{
unsigned char type; //类型
unsigned char code; //代码
unsigned short checksum; //校验和
union {
struct {
unsigned short id; //标识符
unsigned short sequence; //序号
} echo;
unsigned long gateway; //目标路由器的IP地址
struct {
unsigned short unused; //
unsigned short mtu;
} frag;
} un;
} icmphdr;
/*
IGMP Header Format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Type | Code | Checksum |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Identifier |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Group Address |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| |
Access Key
| |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IGMP Header FormatRFC-988
--*/
typedef
struct _igmphdr
{
unsigned char type;
unsigned char code; /* For newer IGMP */
unsigned short csum;
unsigned long group;
} igmphdr;
/*
IPv6 Header Format
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Ver. | IHL | TOS | Total Length |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Identification |Flags| Fragment Offset |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|1|1|1|1|1|0| Offset| Reserved | Source IP address part 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|1|1|1|1|1|0| Offset| Reserved | Destination IP address part 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: Options :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: SADDR Code |Len adr. part 2| Source IP address part 2 :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: DADDR Code |Len adr. part 2| Destination IP address part 2 :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
: Data :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IPv6 Header Format
RFC-1365
--*/
typedef
struct _ipv6hdr
{
unsigned char version:4;
unsigned char priority:4;
unsigned char flow_lbl[3];
unsigned short payload_len;
unsigned char nexthdr;
unsigned char hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
} ipv6hdr;
/*
* This is an Ethernet frame header.
*/
typedef
struct _ethhdr
{
unsigned char h_dest[ETH_ALEN]; // destination eth addr
unsigned char h_source[ETH_ALEN]; // source ether addr
unsigned short h_proto; // packet type ID field
} ethhdr;
/* FDDI
Define 802.2 Type 1 header */
struct fddi_8022_1_hdr
{
__u8 dsap; /* destination service access point */
__u8 ssap; /* source service access point */
__u8 ctrl; /* control byte #1 */
} __attribute__ ((packed));
/* Define 802.2 Type 2 header */
struct fddi_8022_2_hdr
{
__u8 dsap; /* destination service access point */
__u8 ssap; /* source service access point */
__u8 ctrl_1; /* control byte #1 */
__u8 ctrl_2; /* control byte #2 */
} __attribute__ ((packed));
/* Define 802.2 SNAP header */
#define FDDI_K_OUI_LEN 3
struct fddi_snap_hdr
{
__u8 dsap; /* always 0xAA */
__u8 ssap; /* always 0xAA */
__u8 ctrl; /* always 0x03 */
__u8 oui[FDDI_K_OUI_LEN]; /* organizational universal id */
__u16 ethertype; /* packet type ID field */
} __attribute__ ((packed));
/* Define FDDI LLC frame header */
struct fddihdr
{
__u8 fc; /* frame control */
__u8 daddr[FDDI_K_ALEN]; /* destination address */
__u8 saddr[FDDI_K_ALEN]; /* source address */
union
{
struct fddi_8022_1_hdr llc_8022_1;
struct fddi_8022_2_hdr llc_8022_2;
struct fddi_snap_hdr llc_snap;
} hdr;
} __attribute__ ((packed));
上一篇:SIP协议简述
下一篇:路由器原理和路由协议、算法详解
文章评论
共有 1 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面