1、TCP/IP协议头部结构体 网络协议结构体定义 1. // i386 is little_endian. 2. #ifndef LITTLE_ENDIAN 3. #define LITTLE_ENDIAN (1) //BYTE ORDER 4. #else 5. #error Redefine LITTLE_ORDER 6. #endif 7. //Mac头部,总长度14字节 8. typedef struct _eth_hdr 9. { 10. unsigned char dstmac[6]; //
2、目标mac地址 11. unsigned char srcmac[6]; //源mac地址 12. unsigned short eth_type; //以太网类型 13. }eth_hdr; 14. //IP头部,总长度20字节 15. typedef struct _ip_hdr 16. { 17. #if LITTLE_ENDIAN 18. unsigned char ihl:4; //首部长度 19. unsigned char version:4, //版本 2
3、0. #else 21. unsigned char version:4, //版本 22. unsigned char ihl:4; //首部长度 23. #endif 24. unsigned char tos; //服务类型 25. unsigned short tot_len; //总长度 26. unsigned short id; //标志 27. unsigned short frag_off; //分片偏移 28.
4、 unsigned char ttl; //生存时间 29. unsigned char protocol; //协议 30. unsigned short chk_sum; //检验和 31. struct in_addr srcaddr; //源IP地址 32. struct in_addr dstaddr; //目的IP地址 33. }ip_hdr; 34. //TCP头部,总长度20字节 35. typedef struct _tcp_hdr 36. { 37.
5、 unsigned short src_port; //源端口号 38. unsigned short dst_port; //目的端口号 39. unsigned int seq_no; //序列号 40. unsigned int ack_no; //确认号 41. #if LITTLE_ENDIAN 42. unsigned char reserved_1:4; //保留6位中的4位首部长度 43. unsigned char thl:4;
6、 //tcp头部长度 44. unsigned char flag:6; //6位标志 45. unsigned char reseverd_2:2; //保留6位中的2位 46. #else 47. unsigned char thl:4; //tcp头部长度 48. unsigned char reserved_1:4; //保留6位中的4位首部长度 49. unsigned char reseverd_2:2; //保留6位中的2位 50. uns
7、igned char flag:6; //6位标志 51. #endif 52. unsigned short wnd_size; //16位窗口大小 53. unsigned short chk_sum; //16位TCP检验和 54. unsigned short urgt_p; //16为紧急指针 55. }tcp_hdr; 56. //UDP头部,总长度8字节 57. typedef struct _udp_hdr 58. { 59. unsi
8、gned short src_port; //远端口号 60. unsigned short dst_port; //目的端口号 61. unsigned short uhl; //udp头部长度 62. unsigned short chk_sum; //16位udp检验和 63. }udp_hdr; 64. //ICMP头部,总长度4字节 65. typedef struct _icmp_hdr 66. { 67. unsigned char icmp_type; //类型 6
9、8. unsigned char code; //代码 69. unsigned short chk_sum; //16位检验和 70. }icmp_hdr; 全面的网络协议结构体定义 1. /*********************************************/ 2. //计算机网络各种协议的结构 3. #define ETHER_ADDR_LEN 6 //NIC物理地址占6字节 4. #define MAXDATA 10240 5. /* 6. 网络实验程序
10、 7. 数据包中的TCP包头,IP包头,UDP包头,ARP包,Ethernet包等. 8. 以及各种表.路由寻址表,地址解析协议表DNS表等 9. */ 10. #define ETHERTYPE_IP 0x0800 //IP Protocal 11. #define ETHERTYPE_ARP 0x0806 //Address Resolution Protocal 12. #define ETHERTYPE_REVARP 0x0835 //Reverse Address Resolution Protocal 逆地址解析协议 13. /**
11、/ 14. //ethernet 15. typedef struct ether_header 16. { 17. u_char ether_dhost[ETHER_ADDR_LEN]; 18. u_char ether_shost[ETHER_ADDR_LEN]; 19. u_short ether_type; 20. }ETH_HEADER; 21. /***************************************
12、/ 22. //ether_header eth; 23. /*********************************************/ 24. //arp 25. typedef struct arphdr 26. { 27. u_short ar_hrd; 28. u_short ar_pro; 29. u_char ar_hln; 30. u_char ar_pln; 31. u_short ar_op; 32. }ARP_HEADER; 33
13、 /*********************************************/ 34. /*********************************************/ 35. //IP报头 36. typedef struct ip 37. { 38. u_int ip_v:4; //version(版本) 39. u_int ip_hl:4; //header length(报头长度) 40. u_char ip_tos; 41. u_short ip_len; 4
14、2. u_short ip_id; 43. u_short ip_off; 44. u_char ip_ttl; 45. u_char ip_p; 46. u_short ip_sum; 47. struct in_addr ip_src; 48. struct in_addr ip_dst; 49. }IP_HEADER; 50. /*********************************************/ 51. /************************
15、/ 52. //TCP报头结构体 53. typedef struct tcphdr 54. { 55. u_short th_sport; 56. u_short th_dport; 57. u_int th_seq; 58. u_int th_ack; 59. u_int th_off:4; 60. u_int th_x2:4; 61. u_char th_flags; 62. u_short th_win;
16、63. u_short th_sum; 64. u_short th_urp; 65. }TCP_HEADER; 66. #define TH_FIN 0x01 67. #define TH_SYN 0x02 68. #define TH_RST 0x04 69. #define TH_PUSH 0x08 70. #define TH_ACK 0x10 71. #define TH_URG 0x20 72. /*********************************************/ 73
17、 /*********************************************/ 74. //UDP报头结构体*/ 75. typedef struct udphdr 76. { 77. u_short uh_sport; 78. u_short uh_dport; 79. u_short uh_ulen; 80. u_short uh_sum; 81. }UDP_HEADER; 82. /*********************************************/
18、 83. //============================================= 84. /*********************************************/ 85. /*ARP与ETHERNET生成的报头*/ 86. typedef struct ether_arp 87. { 88. struct arphdr ea_hdr; 89. u_char arp_sha[ETHER_ADDR_LEN]; 90. u_char arp_spa[4]; 91. u_ch
19、ar arp_tha[ETHER_ADDR_LEN]; 92. u_char arp_tpa[4]; 93. }ETH_ARP; 94. #define arp_hrd ea_hdr.ar_hrd 95. #define arp_pro ea_hdr.ar_pro 96. #define arp_hln ea_hdr.ar_hln 97. #define arp_pln ea_hdr.ar_pln 98. #define arp_op ea_hdr.ar_op 99. #define ARPHRD 1 100. /***
20、/ 101. /*********************************************/ 102. //tcp与ip生成的报头 103. typedef struct packet_tcp 104. { 105. struct ip ip; 106. struct tcphdr tcp; 107. u_char data[MAXDATA]; 108. }TCP_IP; 109. /***************
21、/ 110. /*********************************************/ 111. //udp与ip生成的报头 112. typedef struct packet_udp 113. { 114. struct ip ip; 115. struct udphdr udp; 116. }UDP_IP; 117. /*********************************************/ 118. /*****
22、/ 119. //ICMP的各种形式 120. //icmpx,x==icmp_type; 121. //icmp报文(能到达目的地,响应-请求包) 122. struct icmp8 123. { 124. u_char icmp_type; //type of message(报文类型) 125. u_char icmp_code; //type sub code(报文类型子码) 126. u_short icmp_cksum;
23、 127. u_short icmp_id; 128. u_short icmp_seq; 129. char icmp_data[1]; 130. }; 131. //icmp报文(能返回目的地,响应-应答包) 132. struct icmp0 133. { 134. u_char icmp_type; //type of message(报文类型) 135. u_char icmp_code; //type sub code(报文类型子码) 136. u_short icm
24、p_cksum; 137. u_short icmp_id; 138. u_short icmp_seq; 139. char icmp_data[1]; 140. }; 141. //icmp报文(不能到达目的地) 142. struct icmp3 143. { 144. u_char icmp_type; //type of message(报文类型) 145. u_char icmp_code; //type sub code(报文类型子码),例如:0网络原因不能到达,1主机原因不能到达
25、 146. u_short icmp_cksum; 147. u_short icmp_pmvoid; 148. u_short icmp_nextmtu; 149. char icmp_data[1]; 150. }; 151. //icmp报文(重发结构体) 152. struct icmp5 153. { 154. u_char icmp_type; //type of message(报文类型) 155. u_char icmp_code; //type sub c
26、ode(报文类型子码) 156. u_short icmp_cksum; 157. struct in_addr icmp_gwaddr; 158. char icmp_data[1]; 159. }; 160. struct icmp11 161. { 162. u_char icmp_type; //type of message(报文类型) 163. u_char icmp_code; //type sub code(报文类型子码) 164. u_short icmp_cksum
27、 165. u_int icmp_void; 166. char icmp_data[1]; 167. }; ================================================================================ IP协议 IP协议(Internet Protocol)是网络层协议,用在因特网上,TCP,UDP,ICMP,IGMP数据都是按照IP数据格式发送得。IP协议提供的是不可靠无连接得服务。IP数据包由一个头部和一个正文部分构成。正文主要是传输的数据,我们主要
28、来理解头部数据,可以从其理解到IP协议。 IP数据包头部格式(RFC791) Example Internet Datagram Header 上面的就是IP数据的头部格式,这里大概地介绍一下。 IP头部由20字节的固定长度和一个可选任意长度部分构成,以大段点机次序传送,从左到 右。 TCP协议 TCP协议(TRANSMISSION CONTROL PROTOCOL)是传输层协议,为应用层提供服务,和UDP不同的是,TCP协议提供的可靠的面向连接的服务。在RFC793中是基本的TCP描述。关于TCP协议的头部格式内容
29、的说明: TCP Header FORMat TCP Header FORMat 跟IP头部差不多,基本的长度也是20字节。TCP数据包是包含在一个IP数据报文中的。 好了,简单介绍到此为止。来看看我捕获的例子吧。这是一次FTP的连接,呵呵,是cuteftp默认的cute站点,IP地址是:216.3.226.21。我的IP地址假设为:192.168.1.1。下面的数据就是TCO/IP连接过程中的数据传输。我们可以分析TCP/IP协议数据格式以及TCP/IP连接的三次握手(ThreeWay-Handshake)情况。下面的这些十六进制数
30、据只是TCP/IP协议的数据,不是完整的网络通讯数据。 第一次,我向FTP站点发送连接请求(我把TCP数据的可选部分去掉了) 192.168.1.1->216.3.226.21 IP头部: 45 00 00 30 52 52 40 00 80 06 2c 23 c0 a8 01 01 d8 03 e2 15 TCP头部:0d 28 00 15 50 5f a9 06 00 00 00 00 70 02 40 00 c0 29 00 00 来看看IP头部的数据是些什么。 第一字节,“45”,其中“4”是IP协议的版
31、本(Version),说明是IP4。“5”是IHL位,表示IP头部的长度,是一个4bit字段,最大就是1111了,值为12,IP头部的最大长度就是60字节。而这里为“5”,说明是20字节,这是标准的IP头部长度,头部报文中没有发送可选部分数据。 接下来的一个字节“00”是服务类型(Type of Service)。这个8bit字段由3bit的优先权子字段(现在已经被忽略),4 bit的TOS子字段以及1 bit的未用字段(现在为0)构成.4 bit的TOS子字段包含:最小延时、最大吞吐量、最高可靠性以及最小费用构成,这四个1bit位最多只能有一个为1,本例中都为0,表示是一般服务
32、 接着的两个字节“00 30”是IP数据报文总长,包含头部以及数据,这里表示48字节。这48字节由20字节的IP头部以及28字节的TCP头构成(本来截取的TCP头应该是28字节的,其中8字节为可选部分,被我省去了)。因此目前最大的IP数据包长度是65535字节。 再是两个字节的标志位(Identification):“5252”,转换为十进制就是21074。这个是让目的主机来判断新来的分段属于哪个分组。 下一个字节“40”,转换为二进制就是“0100 0000”,其中第一位是IP协议目前没有用上的,为0。接着的是两个标志DF和MF。DF为1表示不要分
33、段,MF为1表示还有进一步的分段(本例为0)。然后的“0 0000”是分段便移(Fragment Offset)。 “80”这个字节就是TTL(Time To Live)了,表示一个IP数据流的生命周期,用Ping显示的结果,能得到TTL的值,很多文章就说通过TTL位来判别主机类型。因为一般主机都有默认的TTL值,不同系统的默认值不一样。比如WINDOWS为128。不过,一般Ping得到的都不是默认值,这是因为每次IP数据包经过一个路由器的时候TTL就减一,当减到0时,这个数据包就消亡了。这也时Tracert的原理。本例中为“80”,转换为十进制就是128了,我用的WIN2000
34、 继续下来的是“06”,这个字节表示传输层的协议类型(Protocol)。在RFC790中有定义,6表示传输层是TCP协议。 “2c 23”这个16bit是头校验和(Header Checksum)。 接下来“c0 a8 01 01”,这个就是源地址(Source Address)了,也就是我的IP地址。 转换为十进制的IP地址就是:192.168.1.1,同样,继续下来的32位“d8 03 e2 15”是目标地址,216.3.226.21 好了,真累啊,终于看完基本的20字节的IP数据报头了。继续看TCP的头部吧,这个是作
35、为IP数据包的数据部分传输的。 TCP头部:0d 28 00 15 50 5f a9 06 00 00 00 00 70 02 40 00 c0 29 00 00 一来就是一个两字节段“0d 28”,表示本地端口号,转换为十进制就是3368。第二个两字节段“00 15”表示目标端口,因为我是连接FTP站点,所以,这个就是21啦,十六进制当然就是“00 15”。 接下来的四个字节“50 5f a9 06”是顺序号(Sequence Number),简写为SEQ,SEQ=1348446470下面的四个字节“00 00 00 00”是确认号(Acknowle
36、dgment Number),简写为ACKNUM。 继续两个字节,“70 02”,转换为二进制吧,“0111 0000 0000 0010”。这两个字节,总共16bit,有好多东西呢。第一个4bit“0111”,是TCP头长,十进制为7,表示28个字节(刚才说了,我省略了8字节的option数据,所以你只看见了20字节)。接着的6bit现在TCP协议没有用上,都为0。最后的6bit“00 0010”是六个重要的标志。这是两个计算机数据交流的信息标志。接收和发送断根据这些标志来确定信息流的种类。下面是一些介绍: URG:(Urgent Pointer field si
37、gnificant)紧急指针。用到的时候值为1,用来处理避免TCP数据流中断 ACK:(Acknowledgment fieldsignificant)置1时表示确认号(AcknowledgmentNumber)为合法,为0的时候表示数据段不包含确认信息,确认号被忽略。 PSH:(Push Function),PUSH标志的数据,置1时请求的数据段在接收方得到后就可直接送到应用程序,而不必等到缓冲区满时才传送。 RST:(Reset the connection)用于复位因某种原因引起出现的错误连接,也用来拒绝非法数据和请求。如果接收到RST位时候,通
38、常发生了某些错误。 SYN:(Synchronize sequence numbers)用来建立连接,在连接请求中,SYN=1,ACK=0,连接响应时,SYN=1,ACK=1。即,SYN和ACK来区分Connection Request和Connection Accepted。 FIN:(No more data from sender)用来释放连接,表明发送方已经没有数据发送了。 这6个标志位,你们自己对号入座吧。本例中SYN=1,ACK=0,当然就是表示连接请求了。我们可以注意下面两个过程的这两位的变换。 后面的“40 00 c0 2
39、9 00 00”不讲了,呵呵,偷懒了。后面两次通讯的数据,自己分开看吧。我们看看连接的过程,一些重要地方的变化。 第二次,FTP站点返回一个可以连接的信号。 216.3.226.21->192.168.1.1 IP头部: 45 00 00 2c c6 be 40 00 6a 06 cd ba d8 03 e2 15 c0 a8 01 01 TCP头部:00 15 0d 28 4b 4f 45 c1 50 5f a9 07 60 12 20 58 64 07 00 00 第三次,我确认连接。TCP连接建立起来。
40、192.168.1.1->216.3.226.21 IP头部: 45 00 00 28 52 53 40 00 80 06 2c 2a c0 a8 01 01 d8 03 e2 15 TCP头部:0d 28 00 15 50 5f a9 07 4b 4f 45 c2 50 10 40 b0 5b 1c 00 00 好,我们看看整个Threeway_handshake过程。 第一步,我发出连接请求,TCP数据为:SEQ=50 5f a9 06,ACKNUM=00 00 00 00,SYN=1,ACK=0。 第二步,对方确认可以
41、连接,TCP数据为:SEQ=4b 4f 45 c1,ACKNUM=50 5f a9 07,SYN=1,ACK=1。 第三步,我确认建立连接。SEQ=50 5f a9 07, ACKNUM=4b 4f45c2,SYN=0,ACK=1。 可以看出什么变化么?正式建立连接了呢,这些东西是什么值? 我接收从216.3.226.21->192.168.1.1的下一个数据包中: SEQ=4b 4f 45 c2,ACKNUM=50 5f a9 07,SYN=0,ACK=1这些都是很基础的东西,对于编写sniffer这样的东西是必须非常熟悉的。这里只讲解了TCP/IP协议的一点点东西,主要是头部数据的格式。(T002) =============================================================================== 附加图片,直观具体,帮助理解






