我是靠谱客的博主 聪明小笼包,最近开发中收集的这篇文章主要介绍TCPIP常用协议头格式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

李国帅于2008.6编辑

包含TCP,UDP,IP,IPv6,Ethernet frame,FDDI 802等常见帧结构



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

/*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

/*RFC-768--*/
typedef
struct _udphdr
{
unsigned short source;
unsigned short dest;
unsigned short len;
unsigned short check;}udphdr;

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

/*
RFC-988
--*/
typedef
struct _igmphdr
{
unsigned char type;
unsigned char code; /* For newer IGMP */
unsigned short csum;
unsigned long group;
} igmphdr;

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;

Ethernet frame header

/*
* 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 802.2

/*
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));

最后

以上就是聪明小笼包为你收集整理的TCPIP常用协议头格式的全部内容,希望文章能够帮你解决TCPIP常用协议头格式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部