常见tcp/ip协议结构定义

常见tcp/ip协议结构定义

 

None.gif #ifndef _PROTO_H_
None.gif
#define  _PROTO_H_
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifTCP/IP 协议类型
ExpandedBlockEnd.gif
*/

None.gif
#define  IPPROTO_IP        0               //  IP
None.gif
#define  IPPROTO_ICMP    1               //  ICMP
None.gif
#define  IPPROTO_TCP        6               //  TCP
None.gif
#define  IPPROTO_UDP        17              //  UDP
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif* 常见端口
ExpandedBlockEnd.gif
*/

None.gif
#define  PORT_DNS        53                 //  DNS
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif* 其它定义
ExpandedBlockEnd.gif
*/

None.gif
#define  ETH_ALEN       6               //  以太网地址大小
None.gif
#define  ETH_HLEN       14              //  以太网头部大小
None.gif
#define  ETH_DATA_LEN   1500            //  最大帧负载数据大小
None.gif
#define  ETH_FRAME_LEN  1514            //  最大帧大小,头部+负载数据
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**
InBlock.gif * 常见协议定义
ExpandedBlockEnd.gif*
*/

None.gif#pragma pack(push, 
1 )
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*14字节的以太网包头
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _ETHDR         
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    UCHAR    eh_dst[ETH_ALEN];            
// 目的MAC地址
InBlock.gif
    UCHAR    eh_src[ETH_ALEN];            // 源MAC地址
InBlock.gif
    USHORT    eh_type;                    // 下层协议类型,如IP(ETHERTYPE_IP)、ARP(ETHERTYPE_ARP)等
ExpandedBlockEnd.gif
}
 ETHDR,  * PETHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*28字节的ARP头
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _ARPHDR    
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    USHORT    ar_hrd;                
//    硬件地址类型,以太网中为ARPHRD_ETHER
InBlock.gif
    USHORT    ar_pro;                //  协议地址类型,ETHERTYPE_IP
InBlock.gif
    UCHAR    ar_hln;                //    硬件地址长度,MAC地址的长度为6
InBlock.gif
    UCHAR    ar_pln;                //    协议地址长度,IP地址的长度为4
InBlock.gif
    USHORT    ar_op;                //    ARP操作代码,ARPOP_REQUEST为请求,ARPOP_REPLY为响应
InBlock.gif
    UCHAR    ar_sha[ETH_ALEN];    //    源MAC地址
InBlock.gif
    ULONG    ar_sip;                //    源IP地址
InBlock.gif
    UCHAR    ar_tha[ETH_ALEN];    //    目的MAC地址
InBlock.gif
    ULONG    ar_tip;                //    目的IP地址
ExpandedBlockEnd.gif
}
 ARPHDR,  * PARPHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*20字节的IP头
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _IPHDR        
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    UCHAR    h_lenver;            
// 版本号和头长度(各占4位)
InBlock.gif
    UCHAR    tos;                // 服务类型 
InBlock.gif
    USHORT    total_len;            // 封包总长度,即整个IP报的长度
InBlock.gif
    USHORT    ident;                // 封包标识,惟一标识发送的每一个数据报
InBlock.gif
    USHORT    frag_and_flags;        // 标志
InBlock.gif
    UCHAR    ttl;                // 生存时间,就是TTL
InBlock.gif
    UCHAR    protocol;            // 协议,可能是TCP、UDP、ICMP等
InBlock.gif
    USHORT    checksum;            // 校验和
InBlock.gif
    ULONG    saddr;                // 源IP地址
InBlock.gif
    ULONG    daddr;                // 目标IP地址
ExpandedBlockEnd.gif
}
 IPHDR,  * PIPHDR; 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*20字节的TCP
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _TCPHDR    
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    USHORT    srceport;            
// 16位源端口号
InBlock.gif
    USHORT    dstport;            // 16位目的端口号
InBlock.gif
    ULONG    seq;                // 32位序列号
InBlock.gif
    ULONG    ack;                // 32位确认号
InBlock.gif
    UCHAR    dataoffset;            // 高4位表示数据偏移
InBlock.gif
    UCHAR    flags;                // 6位标志位
InBlock.gif    
//FIN - 0x01
InBlock.gif    
//SYN - 0x02
InBlock.gif    
//RST - 0x04 
InBlock.gif    
//PSH - 0x08
InBlock.gif    
//ACK - 0x10
InBlock.gif    
//URG - 0x20
InBlock.gif    
//ACE - 0x40
InBlock.gif    
//CWR - 0x80
InBlock.gif

InBlock.gif    USHORT    window;                
// 16位窗口大小
InBlock.gif
    USHORT    checksum;            // 16位校验和
InBlock.gif
    USHORT    urgptr;                // 16位紧急数据偏移量 
ExpandedBlockEnd.gif
}
 TCPHDR,  * PTCPHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*伪TCP头,计算校验和时使用
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _PSDTCPHDR
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ULONG    saddr;
InBlock.gif    ULONG    daddr;
InBlock.gif    
char    mbz;
InBlock.gif    
char    ptcl;
InBlock.gif    USHORT    tcpl;
ExpandedBlockEnd.gif}
 PSDTCPHDR,  * PPSDTCPHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*8字节的UDP头
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _UDPHDR
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    USHORT    srcport;            
// 源端口号        
InBlock.gif
    USHORT    dstport;            // 目的端口号        
InBlock.gif
    USHORT    len;                // 封包长度
InBlock.gif
    USHORT    checksum;            // 校验和
ExpandedBlockEnd.gif
}
 UDPHDR,  * PUDPHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*伪UDP头,计算校验和时使用
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _PSDUDPHDR
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ULONG    saddr;
InBlock.gif    ULONG    daddr;
InBlock.gif    
char    mbz;
InBlock.gif    
char    ptcl;
InBlock.gif    USHORT    udpl;
ExpandedBlockEnd.gif}
 PSDUDPHDR,  * PPSDUDPHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*12字节的ICMP头
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _ICMPHDR
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    UCHAR   type;                
//类型
InBlock.gif
    UCHAR   code;                //代码
InBlock.gif
    USHORT  checksum;            //校验和
InBlock.gif
    USHORT  id;                    //标识符
InBlock.gif
    USHORT  sequence;            //序列号
InBlock.gif
    ULONG   timestamp;            //时间戳
ExpandedBlockEnd.gif
}
 ICMPHDR,  * PICMPHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif*6字节的PPPOE头+2字节协议
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _PPPOEHDR
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    UCHAR    ver_type;            
//版本+类型 一般为0x11
InBlock.gif
    UCHAR    code;                //编码
InBlock.gif
    USHORT    sessionid;            //session id
InBlock.gif
    USHORT    len;                //长度
InBlock.gif
    USHORT    protocol;            //协议
ExpandedBlockEnd.gif
}
 PPPOEHDR,  * PPPPOEHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif* dns包头
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _DNSHDR 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    USHORT id;
InBlock.gif    USHORT flags;
InBlock.gif    USHORT quests;
InBlock.gif    USHORT answers;
InBlock.gif    USHORT author;
InBlock.gif    USHORT addition;
ExpandedBlockEnd.gif}
 DNSHDR,  * PDNSHDR;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* dns查询包,query
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _DNSQUERY
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*UCHAR *dname;*/    //查询的域名,这是一个大小在0到63之间的字符串
ExpandedSubBlockStart.gifContractedSubBlock.gif
    /**//*该域名的获取方法如下:
InBlock.gif    * 长度:udp包总长度-sizeof(UDPHDR)-sizeof(DNSHDR)-sizeof(DNSQUERY)
InBlock.gif    * 内容在dns头后面
ExpandedSubBlockEnd.gif    
*/

InBlock.gif    USHORT    type;            
//查询类型,大约有20个不同的类型
InBlock.gif
    USHORT    classes;        //查询类,通常是A类既查询IP地址
ExpandedBlockEnd.gif
}
 DNSQUERY,  * PDNSQUERY;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* dns响应包
ExpandedBlockEnd.gif
*/

None.giftypedef 
struct  _DNSRESPONSE
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    USHORT    name;        
// 查询的域名
InBlock.gif
    USHORT    type;        // 查询的类型
InBlock.gif
    USHORT    classes;    // 类型码
InBlock.gif
    UINT    ttl;        // 生存时间
InBlock.gif
    USHORT    length;        // 资源数据长度
InBlock.gif
    UINT    addr;        // 资源数据
ExpandedBlockEnd.gif
}
 DNSRESPONSE,  * PDNRESPONSE;
None.gif
None.gif#pragma pack(pop)
None.gif
None.gif
#endif

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25897606/viewspace-704534/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25897606/viewspace-704534/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值