protoinfo.h
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:6k
开发平台:

Visual C++

  1. //////////////////////////////////////////////////
  2. // protoinfo.h文件
  3. /*
  4. 定义协议格式
  5. 定义协议中使用的宏
  6.   */
  7. #ifndef __PROTOINFO_H__
  8. #define __PROTOINFO_H__
  9. ////////////////////////////////////////////
  10. // 宏定义
  11. /////////////////////////////////////////////////////////
  12. // 协议格式
  13. #define ETHERTYPE_IP    0x0800
  14. #define ETHERTYPE_ARP   0x0806
  15. typedef struct _ETHeader         // 14 bytes
  16. {
  17. UCHAR dhost[6]; // 目的MAC地址destination mac address
  18. UCHAR shost[6]; // 源MAC地址source mac address
  19. USHORT type; // 下层协议类型,如IP(ETHERTYPE_IP)、ARP(ETHERTYPE_ARP)等
  20. } ETHeader, *PETHeader;
  21. #define ARPHRD_ETHER  1
  22. // ARP协议opcodes
  23. #define ARPOP_REQUEST 1 // ARP 请求
  24. #define ARPOP_REPLY 2 // ARP 响应
  25. typedef struct _ARPHeader // 28字节的ARP头
  26. {
  27. USHORT hrd; // 硬件地址空间,以太网中为ARPHRD_ETHER
  28. USHORT eth_type; //  以太网类型,ETHERTYPE_IP ??
  29. UCHAR maclen; // MAC地址的长度,为6
  30. UCHAR iplen; // IP地址的长度,为4
  31. USHORT opcode; // 操作代码,ARPOP_REQUEST为请求,ARPOP_REPLY为响应
  32. UCHAR smac[6]; // 源MAC地址
  33. UCHAR saddr[4]; // 源IP地址
  34. UCHAR dmac[6]; // 目的MAC地址
  35. UCHAR daddr[4]; // 目的IP地址
  36. } ARPHeader, *PARPHeader;
  37. //Protocol
  38. #define PROTO_ICMP    1
  39. #define PROTO_IGMP    2
  40. #define PROTO_TCP     6
  41. #define PROTO_UDP     17
  42. typedef struct _IPHeader // 20
  43. {
  44.     UCHAR     iphVerLen;      // 版本号和头长度(各占4位)
  45.     UCHAR     ipTOS;          // 服务类型 
  46.     USHORT    ipLength;       // 封包总长度,即整个IP报的长度
  47.     USHORT    ipID;   // 封包标识,惟一标识发送的每一个数据报
  48.     USHORT    ipFlags;       // 标志
  49.     UCHAR     ipTTL;       // 生存时间,就是TTL
  50.     UCHAR     ipProtocol;     // 协议,可能是TCP、UDP、ICMP等
  51.     USHORT    ipChecksum;     // 校验和
  52.     ULONG     ipSource;       // 源IP地址
  53.     ULONG     ipDestination;  // 目标IP地址
  54. } IPHeader, *PIPHeader; 
  55. //  define the tcp flags....
  56. #define   TCP_FIN   0x01
  57. #define   TCP_SYN   0x02
  58. #define   TCP_RST   0x04
  59. #define   TCP_PSH   0x08
  60. #define   TCP_ACK   0x10
  61. #define   TCP_URG   0x20
  62. #define   TCP_ACE   0x40
  63. #define   TCP_CWR   0x80
  64. typedef struct _TCPHeader  //20 bytes
  65. {
  66. USHORT sourcePort; // 16位源端口号
  67. USHORT destinationPort; // 16位目的端口号
  68. ULONG sequenceNumber; // 32位序列号
  69. ULONG acknowledgeNumber; // 32位确认号
  70. UCHAR dataoffset; // 高4位表示数据偏移
  71. UCHAR flags; // 6位标志位
  72. //FIN - 0x01
  73. //SYN - 0x02
  74. //RST - 0x04 
  75. //PUSH- 0x08
  76. //ACK- 0x10
  77. //URG- 0x20
  78. //ACE- 0x40
  79. //CWR- 0x80
  80. USHORT windows; // 16位窗口大小
  81. USHORT checksum; // 16位校验和
  82. USHORT urgentPointer; // 16位紧急数据偏移量 
  83. } TCPHeader, *PTCPHeader;
  84. typedef struct _UDPHeader
  85. {
  86. USHORT sourcePort; // 源端口号
  87. USHORT destinationPort;// 目的端口号
  88. USHORT len; // 封包长度
  89. USHORT checksum; // 校验和
  90. } UDPHeader, *PUDPHeader;
  91. typedef struct _ICMPHeader
  92. {
  93.     UCHAR   type;
  94.     UCHAR   code;
  95.     USHORT  checksum;
  96.     USHORT  id;
  97.     USHORT  sequence;
  98.     ULONG   timestamp;
  99. } ICMPHeader, *PICMPHeader;
  100. #endif // __PROTOINFO_H__
  101. /*
  102. #define ETHER_LENGTH  14
  103. #define IP_LENGTH     20
  104. #define TCP_LENGTH    20
  105. #define UDP_LENGTH    8
  106. #define ICMP_LENGTH   8
  107. #define ARP_LENGTH    28
  108. #define PSEUDO_LENGTH 12
  109. #define DATA_LENGTH   32
  110. typedef struct ether_hdr // 14 bytes
  111. {
  112. u_char dhost[6]; //destination mac address
  113. u_char shost[6]; //source mac address
  114. u_short type;               //IP ,ARP , RARP       Next Layer Protocol IP:0800 ARP:0806
  115. } ETHER_HDR, *PETHER_HDR;
  116. // 4 byte ip address
  117. typedef struct ip_addr
  118. {
  119. u_char byte1;
  120. u_char byte2;
  121. u_char byte3;
  122. u_char byte4;
  123. } IP_ADDR, *PIP_ADDR;
  124. typedef struct arp_hdr   //28 bytes ARP_LENGTH
  125. {
  126. u_short hrd;       //hardware address space=0x0001
  127. u_short eth_type;  //Ethernet type ....=0x0800
  128. u_char maclen;     //Length of mac address=6
  129. u_char iplen;      //Length of ip addres=4
  130. u_short opcode;    //Request =1 Reply=2 (highbyte)
  131. u_char smac[6]; //source mac address
  132. ip_addr saddr; //Source ip address
  133. u_char dmac[6];    //Destination mac address
  134. ip_addr daddr; //Destination ip address
  135. } ARP_HDR, *PARP_HDR;
  136. typedef struct ip_hdr
  137. {
  138.     unsigned char  ip_verlen;        // 4-bit IPv4 version
  139. // 4-bit header length (in 32-bit words)
  140.     unsigned char  ip_tos;           // IP type of service
  141.     unsigned short ip_totallength;   // Total length
  142.     unsigned short ip_id;            // Unique identifier 
  143.     unsigned short ip_offset;        // Fragment offset field
  144.     unsigned char  ip_ttl;           // Time to live
  145.     unsigned char  ip_protocol;      // Protocol(TCP,UDP etc)
  146.     unsigned short ip_checksum;      // IP checksum
  147.     unsigned int   ip_srcaddr;       // Source address
  148.     unsigned int   ip_destaddr;      // Source address
  149. } IPV4_HDR, *PIPV4_HDR, FAR * LPIPV4_HDR;
  150. typedef struct tcp_hdr  //20 bytes
  151. {
  152. u_short sport;      //Source port
  153. u_short dport;      //Destination port
  154. u_long seqno;       //Sequence no
  155. u_long ackno;       //Ack no
  156. u_char offset;      //Higher level 4 bit indicates data offset
  157. u_char flag;        //Message flag
  158. //FIN - 0x01
  159. //SYN - 0x02
  160. //RST - 0x04 
  161. //PUSH- 0x08
  162. //ACK- 0x10
  163. //URG- 0x20
  164. //ACE- 0x40
  165. //CWR- 0x80
  166. u_short win;
  167. u_short checksum;
  168. u_short uptr;
  169. } TCP_HDR, *PTCP_HDR;
  170. //For checksum calculation purpose
  171. struct pseudo_hdr  //12 bytes 
  172. {
  173.     unsigned int  saddr;      // Source address
  174.     unsigned int  daddr;      // Destination address
  175. u_char zero;
  176. u_char  proto;          // Protocol
  177. u_short tcp_len;
  178. tcp_hdr tcp;
  179. };
  180. typedef struct icmp_hdr
  181. {
  182.     unsigned char   icmp_type;
  183.     unsigned char   icmp_code;
  184.     unsigned short  icmp_checksum;
  185.     unsigned short  icmp_id;
  186.     unsigned short  icmp_sequence;
  187.     unsigned long   icmp_timestamp;
  188. } ICMP_HDR, *PICMP_HDR, FAR *LPICMP_HDR;
  189.   */