protoinfo.h
上传用户:zhangjjyh
上传日期:2021-11-11
资源大小:1251k
文件大小:3k
源码类别:

P2P编程

开发平台:

Objective-C

  1. //////////////////////////////////////////////////
  2. // protoinfo.h文件
  3. /*
  4. 定义协议格式
  5. 定义协议中使用的宏
  6.  */
  7. #ifndef __PROTOINFO_H__
  8. #define __PROTOINFO_H__
  9. #define ETHERTYPE_IP    0x0800
  10. #define ETHERTYPE_ARP   0x0806
  11. typedef struct _ETHeader         // 14字节的以太头
  12. {
  13. UCHAR dhost[6]; // 目的MAC地址destination mac address
  14. UCHAR shost[6]; // 源MAC地址source mac address
  15. USHORT type; // 下层协议类型,如IP(ETHERTYPE_IP)、ARP(ETHERTYPE_ARP)等
  16. } ETHeader, *PETHeader;
  17. #define ARPHRD_ETHER  1
  18. // ARP协议opcodes
  19. #define ARPOP_REQUEST 1 // ARP 请求
  20. #define ARPOP_REPLY 2 // ARP 响应
  21. typedef struct _ARPHeader // 28字节的ARP头
  22. {
  23. USHORT hrd; // 硬件地址空间,以太网中为ARPHRD_ETHER
  24. USHORT eth_type; //  以太网类型,ETHERTYPE_IP ??
  25. UCHAR maclen; // MAC地址的长度,为6
  26. UCHAR iplen; // IP地址的长度,为4
  27. USHORT opcode; // 操作代码,ARPOP_REQUEST为请求,ARPOP_REPLY为响应
  28. UCHAR smac[6]; // 源MAC地址
  29. UCHAR saddr[4]; // 源IP地址
  30. UCHAR dmac[6]; // 目的MAC地址
  31. UCHAR daddr[4]; // 目的IP地址
  32. } ARPHeader, *PARPHeader;
  33. // 协议
  34. #define PROTO_ICMP    1
  35. #define PROTO_IGMP    2
  36. #define PROTO_TCP     6
  37. #define PROTO_UDP     17
  38. typedef struct _IPHeader // 20字节的IP头
  39. {
  40.     UCHAR     iphVerLen;      // 版本号和头长度(各占4位)
  41.     UCHAR     ipTOS;          // 服务类型 
  42.     USHORT    ipLength;       // 封包总长度,即整个IP报的长度
  43.     USHORT    ipID;   // 封包标识,惟一标识发送的每一个数据报
  44.     USHORT    ipFlags;       // 标志
  45.     UCHAR     ipTTL;       // 生存时间,就是TTL
  46.     UCHAR     ipProtocol;     // 协议,可能是TCP、UDP、ICMP等
  47.     USHORT    ipChecksum;     // 校验和
  48.     ULONG     ipSource;       // 源IP地址
  49.     ULONG     ipDestination;  // 目标IP地址
  50. } IPHeader, *PIPHeader; 
  51. // 定义TCP标志
  52. #define   TCP_FIN   0x01
  53. #define   TCP_SYN   0x02
  54. #define   TCP_RST   0x04
  55. #define   TCP_PSH   0x08
  56. #define   TCP_ACK   0x10
  57. #define   TCP_URG   0x20
  58. #define   TCP_ACE   0x40
  59. #define   TCP_CWR   0x80
  60. typedef struct _TCPHeader // 20字节的TCP头
  61. {
  62. USHORT sourcePort; // 16位源端口号
  63. USHORT destinationPort; // 16位目的端口号
  64. ULONG sequenceNumber; // 32位序列号
  65. ULONG acknowledgeNumber; // 32位确认号
  66. UCHAR dataoffset; // 高4位表示数据偏移
  67. UCHAR flags; // 6位标志位
  68. //FIN - 0x01
  69. //SYN - 0x02
  70. //RST - 0x04 
  71. //PUSH- 0x08
  72. //ACK- 0x10
  73. //URG- 0x20
  74. //ACE- 0x40
  75. //CWR- 0x80
  76. USHORT windows; // 16位窗口大小
  77. USHORT checksum; // 16位校验和
  78. USHORT urgentPointer; // 16位紧急数据偏移量 
  79. } TCPHeader, *PTCPHeader;
  80. typedef struct _UDPHeader
  81. {
  82. USHORT sourcePort; // 源端口号
  83. USHORT destinationPort;// 目的端口号
  84. USHORT len; // 封包长度
  85. USHORT checksum; // 校验和
  86. } UDPHeader, *PUDPHeader;
  87. #endif // __PROTOINFO_H__