三次握手和四次挥手以及TCP标志位的详细介绍

2年前 (2022) 程序员胖胖胖虎阿
229 0 0

<section id="nice" style="padding: 10px; line-height: 1.6; word-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 15px; letter-spacing: 0.05em; color: #595959;"><h2 style="margin-top: 40px; margin-bottom: 20px; bmin-height: 32px; line-height: 32px; border-bottom: solid 1px #000000; color: #35b378; display: inline-block; border-bottom-width: 0px; border-bottom-style: solid; border-color: #35b378; padding-top: 5px; padding-right: 0.5em; padding-left: 0.5em; font-size: 23px; margin: 1em 0 0rem 0; padding: 0.5em 0; text-align: leftt; font-weight: bold;"><span>一、TCP标志位</span></h2> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">在讲TCP三次握手和四次挥手之前,先说一下TCP标志位,方便后续的理解。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">简单来说,TCP标志位的值代表了当前请求的目的。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">标志位一共有6种,分别是:</p> <ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: decimal;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">SYN(synchronous): 发送/同步标志,用来建立连接,和下面的第二个标志位ACK搭配使用。连接开始时,SYN=1,ACK=0,代表连接开始但是未获得响应。当连接被响应的时候,标志位会发生变化,其中ACK会置为1,代表确认收到连接请求,此时的标志位变成了 SYN=1,ACK=1。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">ACK(acknowledgement):确认标志,表示确认收到请求。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">PSH(push) :表示推送操作,就是指数据包到达接收端以后,不对其进行队列处理,而是尽可能的将数据交给应用程序处理;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">FIN(finish):结束标志,用于结束一个TCP会话;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">RST(reset):重置复位标志,用于复位对应的TCP连接。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">URG(urgent):紧急标志,用于保证TCP连接不被中断,并且督促中间层设备尽快处理。</section></li></ol> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">此外,还有两个序号:</p> <ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: decimal;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">Sequence number :顺序号,发送数据包中的第一个字节的序列号,一般为小写的seq。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">Acknowledge number:确认号,响应前面的seq,值为seq+1,可以理解为期望下次发出的序列号为seq+1;</section></li></ol> <h2 style="margin-top: 40px; margin-bottom: 20px; bmin-height: 32px; line-height: 32px; border-bottom: solid 1px #000000; color: #35b378; display: inline-block; border-bottom-width: 0px; border-bottom-style: solid; border-color: #35b378; padding-top: 5px; padding-right: 0.5em; padding-left: 0.5em; font-size: 23px; margin: 1em 0 0rem 0; padding: 0.5em 0; text-align: leftt; font-weight: bold;"><span>二、TCP三次握手</span></h2> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>1.TCP三次握手概述</span></h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包。 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方的顺序号和确认号并交换 TCP信息</p> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>2.图解TCP三次握手</span></h3> <figure style="margin: 0; margin-top: 10px; margin-bottom: 10px;"><img src="https://image.135editor.com/files/users/749/7492441/201911/9s7eBDgK_XqV8.png" alt style="display: block; margin: 0 auto; width: 100%;"></figure> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">第一次握手:客户端Client发送位码为SYN=1,随机产生seq=x的数据包到服务器,服务器Server由SYN=1知道,客户端Client要求建立联机;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">第二次握手:服务器Server收到请求后要确认联机信息,向客户端Client发送ack=(客户端Client请求连接时的seq)+1,SYN=1,ACK=1,产生seq=y的包,代表接收到连接请求并且向客户端再次确认;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">第三次握手:客户端Client收到后检查ack是否正确,即第一次发送的seq+1,以及位码ACK是否为1,代表收到了服务器端发过来的确认信息。之后客户端Client会再向服务器发送ack=(服务器Server的seq+1),ACK=1,服务器Server收到后确认ack 值与ACK=1,连接建立成功。</section></li></ul> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>3.针对TCP连接的安全问题:SYN攻击</span></h3> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">危害:SYN攻击属于DOS攻击的一种,它利用TCP协议缺陷,通过发送大量的半连接请求,耗费CPU和内存资源。SYN攻击除了能影响主机外,还可以危害路由器、防火墙等网络系统,事实上SYN攻击并不管目标是什么系统,只要这些系统打开TCP服务就可以实施。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">原理:在三次握手过程中,服务器发送SYN-ACK(确认收到客户端请求的连接)之后,收到客户端的ACK(第三个包)之前的TCP连接称为半连接(half-open connect).此时服务器处于SYN_RECV(等待客户端相应)状态,如果接收到客户端的ACK,则TCP连接成功,如果未接受到,则会重发请求直至成功。SYN攻击就是 攻击客户端 在短时间内伪造大量不存在的IP地址,向服务器不断地发送SYN包,服务器回复确认包,并等待客户的确认,由于源地址是不存在的,服务器需要不断的重发直 至超时,这些伪造的SYN包将长时间占用未连接队列,影响了正常的SYN,目标系统运行缓慢,严重者引起网络堵塞甚至系统瘫痪。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">检测:检测SYN攻击非常的方便,当在服务器上看到大量的半连接状态时,特别是源IP地址是随机的,基本上可以断定这是一次SYN攻击。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">防范:主要有两大类,一类是通过防火墙、路由器等过滤网关防护,另一类是通过加固TCP/IP协议栈防范.但必须清楚的是,SYN攻击不能完全被阻止,我们所做的是尽可能的减轻SYN攻击的危害,除非将TCP协议重新设计。</section></li></ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">过滤网关防护:</p> <ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: decimal;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">网关超时设置</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">SYN网关</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">SYN代理</section></li></ol> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">加固TCP/IP协议栈:</p> <ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: decimal;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">SynAttackProtect机制</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">SYN cookies技术</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">增加最大半连接数</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">缩短超时时间</section></li></ol> <h2 style="margin-top: 40px; margin-bottom: 20px; bmin-height: 32px; line-height: 32px; border-bottom: solid 1px #000000; color: #35b378; display: inline-block; border-bottom-width: 0px; border-bottom-style: solid; border-color: #35b378; padding-top: 5px; padding-right: 0.5em; padding-left: 0.5em; font-size: 23px; margin: 1em 0 0rem 0; padding: 0.5em 0; text-align: leftt; font-weight: bold;"><span>三、图解TCP四次挥手</span></h2> <figure style="margin: 0; margin-top: 10px; margin-bottom: 10px;"><img src="https://image.135editor.com/files/users/749/7492441/201911/O2bTZ8HU_Whar.png" alt style="display: block; margin: 0 auto; width: 100%;"></figure> <ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: decimal;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">客户端Client进程发出连接释放报文,并且停止发送数据。其中FIN=1,顺序号为seq=m(等于前面已经传送过来的数据的最后一个字节的序号加1),此时,客户端Client进入FIN-WAIT-1(终止等待1)状态。 TCP规定,FIN报文段即使不携带数据,也要消耗一个序号。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">服务器Server收到连接释放报文,发出确认报文,ACK=1,ack=m+1,并且带上自己的顺序号seq=n,此时,服务器Server就进入了CLOSE-WAIT(关闭等待)状态。TCP服务器通知高层的应用进程,客户端Client向服务器的方向就释放了,这时候处于半关闭状态,即客户端Client已经没有数据要发送了,但是服务器Server若发送数据,客户端Client依然要接受。这个状态还要持续一段时间,也就是整个CLOSE-WAIT状态持续的时间。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">客户端Client收到服务器Server的确认信息后,此时,客户端Client就进入FIN-WAIT-2(终止等待2)状态,等待服务器Server发送连接释放报文(在这之前还需要接受服务器Server发送的最后的数据)。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">服务器Server将最后的数据发送完毕后,就向客户端发送连接释放报文,FIN=1,ack=m+1,由于在半关闭状态,服务器Server很可能又发送了一些数据,假定此时的顺序号为seq=p,此时,服务器Server就进入了LAST-ACK(最后确认)状态,等待客户端Client的确认。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">客户端Client收到服务器Server的连接释放报文后,必须发出确认,ACK=1,ack=p+1,而自己的顺序号是seq=m+1,此时,客户端Client就进入了TIME-WAIT(时间等待)状态。注意此时TCP连接还没有释放,必须经过2*MSL(最长报文段寿命)的时间后,当客户端Client撤销相应的TCB(保护程序)后,才进入CLOSED状态。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; margin: 10px 0;">服务器Server只要收到了客户端Client发出的确认,立即进入CLOSED状态。同样,撤销TCB后,就结束了这次的TCP连接。可以看到,服务器Server结束TCP连接的时间要比客户端Client早一些。</section></li></ol> <h2 style="margin-top: 40px; margin-bottom: 20px; bmin-height: 32px; line-height: 32px; border-bottom: solid 1px #000000; color: #35b378; display: inline-block; border-bottom-width: 0px; border-bottom-style: solid; border-color: #35b378; padding-top: 5px; padding-right: 0.5em; padding-left: 0.5em; font-size: 23px; margin: 1em 0 0rem 0; padding: 0.5em 0; text-align: leftt; font-weight: bold;"><span>四、常见面试题</span></h2> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>1.为什么连接的时候是三次握手,关闭的时候却是四次握手?</span></h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">答:因为当客户端发起关闭连接的请求时,发出的FIN,仅代表客户端没有需要发送给服务器端的数据了。而如果服务器端如果仍有数据需要发送给客户端的话,响应报文ACK和结束报文FIN则就不能同时发送给客户端了。此时,服务器端会先返回一个响应报文,代表接收到了客户端发出的FIN请求,而后在数据传输完了之后,再发出FIN请求,表示服务器端已经准备好断开连接了。所以关闭连接的时候是四次握手。</p> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>2.为什么TIME_WAIT状态需要经过2MSL(最大报文段生存时间)才能返回到CLOSE状态?</span></h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">答:按照前面所说,当四个报文全部发送完毕后,理论上就算是结束了。但是实际情况往往不会那么可靠,比如最后一条报文发出后丢失了,那么服务器端就不会接收到这一报文,每隔一段时间,服务器端会再次发出FIN报文,此时如果客户端已经断开了,那么就无法响应服务器的二次请求,这样服务器会继续发出FIN报文,从而变成了死循环。所以需要设置一个时间段,如果在这个时间段内接收到了服务器端的再次请求,则代表客户端发出的ACK报文没有接收成功。反之,则代表服务器端成功接收响应报文,客户端进入CLOSED状态,此次连接成功关闭。而这个时间,就规定为了2MSL,即客户端发出ACK报文到服务器端的最大时间 + 服务器没有接收到ACK报文再次发出FIN的最大时间 = 2MSL</p> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>3.为什么不能用两次握手进行连接?</span></h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">答:三次握手有两个重要的功能,一是要双方做好发送数据的准备工作且双方都知道彼此已准备好,二要允许双方就初始顺序号进行协商,这个顺序号在握手过程中被发送和确认。如果改为了两次握手,是有可能发生死锁的。在两次握手的设定下,服务器端在成功接受客户端的连接请求SYN后,向客户端发出ACK确定报文时,如果因为网络原因客户端没有接收到,则会一直等待服务器端的ACK报文,而服务器端则认为连接成功建立了,便开始向客户端发送数据。但是客户端因为没有收到服务器端的ACK报文,且不知道服务器的顺序号seq,则会认为连接未成功建立,忽略服务器发出的任何数据。如此客户端一直等待服务器端的ACK报文,而服务器端因为客户端一直没有接收数据,而不断地重复发送数据,从而造成死锁。</p> <h3 style="margin-top: 40px; margin-bottom: 20px; font-size: 20px; margin: 1.2em 0 1em; padding: 0; font-weight: bold; color: #35b378;"><span>4.如果已经建立了连接,但是客户端突然出现故障了怎么办?</span></h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">答:TCP还设有一个保活计时器,显然,客户端如果出现故障,服务器不能一直等下去,白白浪费资源。服务器每收到一次客户端的请求后都会重新复位这个计时器,时间通常是设置为2小时,若两小时还没有收到客户端的任何数据,服务器就会发送一个探测报文段,以后每隔75秒钟发送一次。若一连发送10个探测报文仍然没反应,服务器就认为客户端出了故障,接着就关闭连接。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;">你好!我是 JHCan333,公众号:<strong style="font-weight: bold; color: #35b378;">爱生活的前端狗</strong>的作者。公众号专注<strong style="font-weight: bold; color: #35b378;">前端工程师</strong>方向,包括但不限于<strong style="font-weight: bold; color: #35b378;">技术提高、职业规划、生活品质、个人理财</strong>等方面,会持续发布优质文章,从各个方面<strong style="font-weight: bold; color: #35b378;">提升</strong>前端开发的<strong style="font-weight: bold; color: #35b378;">幸福感</strong>。关注公众号,我们一起向前走! <img src="https://img2018.cnblogs.com/blog/1806307/201911/1806307-20191126212112375-1961956912.png" alt style="display: block; margin: 0 auto; width: 100%;"></p> </section>

相关文章

暂无评论

暂无评论...