是我们需要,才有了三次握手

Sequence numbers 和 Acknowledgments

  Transmission is made reliable via the use of sequence numbers and
  acknowledgments.  Conceptually, each octet of data is assigned a
  sequence number.  The sequence number of the first octet of data in a
  segment is transmitted with that segment and is called the segment
  sequence number.  Segments also carry an acknowledgment number which
  is the sequence number of the next expected data octet of
  transmissions in the reverse direction.  When the TCP transmits a
  segment containing data, it puts a copy on a retransmission queue and
  starts a timer; when the acknowledgment for that data is received, the
  segment is deleted from the queue.  If the acknowledgment is not
  received before the timer runs out, the segment is retransmitted.

首先我们需要明白TCP如何保证数据传输可靠的,依靠序列号和确认机制来保证

  • 接收方可以通过序列号对重复的数据包进行去重;
  • 发送方会在对应数据包未被 ACK 时进行重复发送;
  • 接收方可以根据数据包的序列号对它们进行重新排序;

但如何保证带有序列号的数据包在互联网中唯一呢,因为如果不唯一,TCP无法识别来自之前实例的重复数据段。引出了 ISN

ISN

  The protocol places no restriction on a particular connection being
  used over and over again.  A connection is defined by a pair of
  sockets.  New instances of a connection will be referred to as
  incarnations of the connection.  The problem that arises from this is
  -- "how does the TCP identify duplicate segments from previous
  incarnations of the connection?"  This problem becomes apparent if the
  connection is being opened and closed in quick succession, or if the
  connection breaks with loss of memory and is then reestablished.  
  
  To avoid confusion we must prevent segments from one incarnation of a
  connection from being used while the same sequence numbers may still
  be present in the network from an earlier incarnation.  We want to
  assure this, even if a TCP crashes and loses all knowledge of the
  sequence numbers it has been using.  When new connections are created,
  an initial sequence number (ISN) generator is employed which selects a
  new 32 bit ISN.  The generator is bound to a (possibly fictitious) 32
  bit clock whose low order bit is incremented roughly every 4
  microseconds.  Thus, the ISN cycles approximately every 4.55 hours.
  Since we assume that segments will stay in the network no more than
  the Maximum Segment Lifetime (MSL) and that the MSL is less than 4.55
  hours we can reasonably assume that ISN's will be unique.

通过一些机制保证了 ISN 的唯一性,我们就需要把这个 ISN 同步给对方。

四次握手

为什么说是四次呢?

    1) A --> B  SYN my sequence number is X
    2) A <-- B  ACK your sequence number is X
    3) A <-- B  SYN my sequence number is Y
    4) A --> B  ACK your sequence number is Y
  • 2 和 3 可以在同一条消息上,就是三次握手。
  A three way handshake is necessary because sequence numbers are not
  tied to a global clock in the network, and TCPs may have different
  mechanisms for picking the ISN's.  The receiver of the first SYN has
  no way of knowing whether the segment was an old delayed one or not,
  unless it remembers the last sequence number used on the connection
  (which is not always possible), and so it must ask the sender to
  verify this SYN.  The three way handshake and the advantages of a
  clock-driven scheme are discussed in 【3】.
  • 这是为什么三次握手是必须的,序列号没办法绑定到全球时钟上,而且不同TCP实现实现不同的ISN机制。同时我们几乎只能通过请求发送方来验证 SYN 的有效性。结合 RESET 可以防止旧连接,半连接等。
    这有一篇论文讨论了三次握手和以时钟为基础的方法的讨论 【3】
    RFC793

总结

It is the implementation of a trade-off between memory and messages to provide information for this checking.

posted on 2025-03-08 22:56  嗯嗯好傅  阅读(20)  评论(0)    收藏  举报