Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1760923
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: LINUX

2012-02-18 23:44:01

1. TCP Three-Way Handshake
1)首先服务器调用 socket, bind, listen, accept被阻塞,属于passive open一端;
2)客户端调用 socket, connect被阻塞(属于active open一端),该函数会向服务器发送一个SYN同步数据包,告诉服务器它要发送的数据的初始序号J;
3)服务器收到客户端的SYN J之后,向客户端发送ACK J+1确认数据报(确认收到的了客户端的ACK J);同时在这个确认数据包中,也附带向客户端发送SYN K同步数据包,告诉客户端它要发送的数据的初始序号;所以这个数据包由两部分 ACK J+1 和 SYN K 组成;
4)客户端收到服务器发送该它的 ACK J+1, SYN K之后,connect函数会向服务器发送ACK K+1确认数据包;之后connect函数返回,连接建立完成。然后服务器收到ACK K+1之后,accept函数会从内核获得一个新的socket连接,accept将返回这个连接的描述符sd;然后服务器用这个新的sd来和客户端交互
The minimum number of packets required for this exchange is three; hence, this is called TCP's three-way handshake.

注意:
要理解这里“同步”的含义是指:客户端要向服务器发送信息,它必须先和服务器沟通好(也就是同步好,达成一致的意思)关于它将要发送的数据报的初始序号,不然服务器不知道该如何接收客户端发送给它的数据报。
反之依然,服务器要想客户端发送信息,它也必须先和客户端沟通好(也就是同步好,达成一致的意思)关于它将要发送给客户端的数据报的初始序号,不然客户端不知道如何接收服务器发送给他的数据报。

“同步”在计算机术语中的意思就是:协调一致,等对方“准备妥当”然后一起前进。比如同步读写文件,是指内核的read和write函数必须和磁盘驱动程序协调好,当数据被写入磁盘之后,write函数才能返回用户态。

TCP三次握手中的“同步”,就是要让对方知道“你将要发送的数据报的初始序号J,让对方知道你的初始序号是J,然后对方就知道了要用J来确认它,并接收”之后,你才能向它发送数据。而“确认”就是告诉发送方我已经知道了你要发送的数据的初始序号J,我将在接收时确认该序号J,你可以开始发送初始序号为J的数据报了。到此客户端的connect函数和服务器端的accept就该返回了。
我们必须要能够深入理解事物的本质!

2. TCP三次握手可能发生的错误
1)客户端发送的 SYN J 可能丢包了,那么TCP软件会自动重传;
2)客户端发送了 SYN J,但是收不到 ACK J+1,我们可以排除丢包的情况,因为丢包会自动重传。那么可能是服务器down了,那么connect函数会返回“Connect Time out..."的错误;也可能是端口搞错了,那么connect会返回“Connect refused..."的错误;
3)客户端发送了 SYN J,但是收到了ICMP“No route...",表示没有路由到达目的地;真实的原因可能是“没有路由”,也可能是“使用了非路由地址”等;

下面我们来考虑“丢包”的情况:
1)客户端发送的 SYN J 丢包了,那么客户端就收不到 SYN J+1,所以它会重传;
2)服务器发送的 SYN J+1, ACK K 丢包了,那么服务器就收不到 ACK K+1,所以它也会重传;
3)客户端发送的 ACK K+1 丢包了,那么服务端就收不到 ACK K+1,那么服务器会认为是它发送的SYN J+1, ACK K 丢包了,所以它会重传SYN J+1, ACK K;
所以:服务器发送的 SYN J+1, ACK K 丢包” 与 “客户端发送的 ACK K+1 丢包”,都会导致服务器“重传SYN J+1, ACK K”。而服务器的这种重传过程可能会延续几分钟,会占据服务器的资源。(实际的情况是导致服务器的“incomplete connection queue”队列爆满,从而会丢弃后面到来的“SYN J”,从而造成无法访问的结果,也就是产生了DDOS的结果)

4. DDOS就是利用了第二种“服务器重传 SYN J+1, ACK K”来进行攻击的:

DDOS 即 Dstribution Denial Of Service,其含义是:分布式的攻击服务器,让服务器忙于不断地重传SYN J+1, ACK K”的状态(因为收不到客户端的 ACK K+1),那么当正常业务的访问,就会没有资源来处理了,所以在正常业务的客户端看来,发现服务器“拒绝对它提供服务了”。所以分布式攻击是黑客的动作,而“拒绝服务”是正常业务的客户端看到的结果

当“TCP的三次握手”的最后一步“服务器没有收到“最后的确认 ACK K+1”,请注意此时客户端的connect函数已经正常返回了,在客户端看来一切正常,连接建立完成了,现在可以开始发送数据了;但是在服务器端却因为没有收到“最后的确认 ACK K+1”而重传“SYN J+1, ACK K”,如果正常的话,那么客户端会重传 ACK K+1,然后在重传发送的业务数据报(注意这里是业务数据报,而不是其它的)。如果不正常(即DDOS),那么就是导致服务器不断的重传“SYN J+1, ACK K”;最后超时而放弃了。

5. TCP Options
Each SYN can contain TCP options. Commonly used options include the following: 
1)MSS option. With this option, the TCP sending the SYN announces its maximum segment size, the maximum amount of data that it is willing to accept in each TCP segment, on this connection. 
The sending TCP uses the receiver's MSS value as the maximum size of a segment that it sends. We will see how to fetch and set this TCP option with the TCP_MAXSEG socket option

2)Window scale option. This newer option specifies that the advertised window in the TCP header must be scaled (left-shifted) by 0~–14 bits, providing a maximum window of almost one gigabyte (65,535 * 2^14).
也就是说,可以将“窗口大小”扩大多少倍。最大的窗口为 65535 * 2^14,因为该参数的占14位。

3)Timestamp option.

The latter two are sometimes are also called the "long fat pipe options," since a network with either a high bandwidth or a long delay is called a long fat pipe.

6. TCP 4次挥手断开连接
TCP 4次挥手3次握手的区别
1)将“ACK”改为“FIN”;
2)挥手比握手多了一次,是因为passive close端的 ACK M+1 和 FIN N 是分开发送的,所以多了一次。而分开发送的原因是:passive close端必须将已经接受的数据处理完成才会发送 FIN N。当然二者也可能是在同一个数据包中发送的
  1. One application calls close first, and we say that this end performs the active close. This end's TCP sends a FIN segment, which means it is finished sending data.

  2. The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an end-of-file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection.

  3. Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.

  4. The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.

Since a FIN and an ACK are required in each direction, four segments are normally required. We use the qualifier "normally" because in some scenarios, the FIN in Step 1 is sent with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive close and could be combined into one segment.

The sending of each FIN occurs when a socket is closed.

either end—the client or the server—can perform the active close. Often the client performs the active close, but with some protocols (notably HTTP/1.0), the server performs the active close.
 

阅读(1076) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~