3
头图

of How Networks Are Connected describes how the protocol stack and network card in the operating system send application messages to the server:

  1. uncreate socket
  2. connect to the server
  3. Send and receive data
  4. Disconnect from the server and delete the socket
  5. Packet sending and receiving operation between IP and Ethernet
  6. Operation of sending and receiving data using UDP

There are many concepts involved: TCP IP Ethernet UDP

This article is based on rfc793 and its subsequent revisions, excerpts some of them, and tries to find usage scenarios in job hunting.

TCP

See appendix for boldface

0. Appendix (basically searched on Baidu Encyclopedia)

  1. Host-to-host: corresponds to the transport layer of the OSI seven-layer model.
  2. Packet switching packet switching: a communication method in which the two communicating parties use a packet as a unit and use a store-and-forward mechanism to realize data exchange. Also known as packet switching, the data communicated by users is divided into multiple smaller data segments of equal length.
    For example: TCP's store-and-forward, TCP has a send and receive buffer, which assembles data into network packets for transmission.
  3. Robustness: Robust and strong means the ability of the system not to crash or crash under abnormal and dangerous conditions.
  4. Congestion congestion: Congestion refers to the phenomenon that the number of packets arriving in the communication subnet is too large to be processed, which causes the performance of this part or even the entire network to degrade. In severe cases, network communication services may even stop. Analogy to traffic jams.
  5. Connection-oriented: connection-oriented, with the following characteristics: establishing a virtual circuit (such as a 3-way handshake), sorting (serial number), acknowledgment (ACK), and flow control.
  6. End-to-end: end-to-end, end-to-end is a network connection. The network needs to communicate, no matter how many machines there are in the middle, as long as the connection is established at both ends (source and destination), that is, end-to-end connection.
  7. Hierarchical structure: The upper layer calls the lower layer, and the lower layer knows nothing about the upper layer. For example, the network seven-layer model
  8. Multi-network: also known as multiple networks, using a variety of communication media network group.
  9. Communication media: also known as transmission media, can be divided into wired (optical fiber, twisted pair, coaxial cable) and wireless
  10. Hardwired: ? ? Can't find information
  11. Circuit switching: mainly used in telephone communication.
  12. Clock-based: clock-based, the clock is the cpu clock, which is the clock that comes with the CPU. The CPU clock cycle, usually the tick pulse or T cycle, is the most basic unit of processing operations. Right-click My Computer --> Properties, you can see the GHz of the computer.
  13. Ethernet: ETHERNET is the most commonly used computer local area network technology.
  14. Packet Switcher: A communication device that applies communication technology to program data information in packets.
  15. Driver: A special program that enables a computer and a device to communicate with each other. It is equivalent to the interface of the hardware. Only through this interface can the operating system control the work of the hardware device.

1 Introduction

Highly reliable, host-to-host protocol for packet-switched computer communication networks.
This article introduces what functions the TCP protocol provides when the program implements TCP, and when the user and the program call the TCP service interface.

1.1 Purpose

It was born to solve the reliability and availability of military communication networks, and is suitable for both government and civilian network communications.
robustness and availability when congested when computer communication systems are unreliable.

TCP a highly reliable protocol for connection , end-to-end Works with hierarchical structure protocol and supports multi-network applications. TCP assumes that the underlying protocols are unreliable. Applicable in principle from hardwired to packet switched or circuit switched networks

                       Protocol Layering

                    +---------------------+
                    |     higher-level    |
                    +---------------------+
                    |        TCP          |
                    +---------------------+
                    |  internet protocol  |
                    +---------------------+
                    |communication network|
                    +---------------------+

1.2 Scope

For multi-network inter-process communication environment and reliable service.

1.3 About Documentation

Describes the specifications for the interaction between TCP services and between TCP and high-level protocols.
The remaining chapters succinct the interface and operation of the protocol
Chapter 2: Summarizes the basic principles of TCP design
Chapter 3: Provides a detailed description of the actions required by TCP when various events occur (arrival of a new segment, user invocation, error, etc.), as well as detailed information on the format of TCP segments.

1.4 Interface

One end of the TCP interface is the user or application program, and the other end is the underlying protocol such as the IP protocol.

The interface between the application and TCP will be elaborated. An interface contains a set of calls. For example, opening and closing the connection, establishing the sending and receiving data on the connection. There is asynchronous communication between the application and TCP.

The TCP protocol is designed to work in a general Internet environment. This paper assumes that the underlying protocol is IP.

1.5 Operation

As mentioned above, the main purpose of TCP is to provide reliable and secure logic circuits and connection services between process pairs.

In order to provide the above services over less reliable Internet communication systems, the following facilities are required.

  • basic data transfer
  • Reliability
  • flow control
  • multiplexing
  • connect
  • priority and safety
1.5.1 Basic Transmission Capability

segment according to a certain byte length, and can transmit continuous octets byte stream bidirectionally.
Usually it's up to TCPs to decide when to block or push data.
TCP defines a push function that pushes data immediately.

1.5.2 Reliability

Purpose : TCP must perform data recovery when data is damaged, lost, duplicated, or transmitted out of order by a network communication system.
scheme :
Lost: The sender assigns a sequence number to each byte, and the receiver returns an acknowledgment (ACK). The receiver does not receive an ACK within the timeout period, and the data will be retransmitted.
and Deduplication: On the receiving end, correctly order the 161fa14661fbc6 segment with the sequence number and deduplicate it.
Corruption: Add a checksum to each transmitted segment, check at the receiving end and discard the corrupted segment.

TCP can recover from errors in network communication systems.

1.5.3 Flow Control

If the sending rate is too fast and it is too late to receive, packets may be lost, retransmitted, and network congestion may occur.
TCP provides a way for the receiver to control the rate of the sender's traffic through a "window" mechanism.

  1. After the receiver receives the data and hands it to the application, it returns the window to the sender.
  2. The sender adjusts the sending rate through the window size.

The window is the size of the current receive buffer.

1.5.4 Multiplexing
  1. Allows multiple processes from a single host to use TCP facilities
  2. One socket for multiple connections

The TCP port and the ip address from the network layer form a socket, a pair of sockets identifies a connection. A socket is multiplexed for multiple connections.

不难发现,多路复用的关键是套接字
套接字:IP地址+端口号
1.5.5 Connection

Reliability and flow control mechanisms require TCPs to initialize and maintain certain state information for each data flow.
This state information, including socket, sequence number, and window size, is called a connection.
A connection is uniquely identified by a pair of sockets on both ends.

When two processes want to communicate, their TCPs must first establish a connection (initialize state information on both sides), and when the communication is complete, the connection is terminated or closed.

Since the connection is established between unreliable hosts and unreliable internet communication systems, in order to avoid incorrect initialization of the connection, a set of clock-based serial number handshake mechanisms are used.

1.5.6 Priority and Security

Users can specify the security and priority of communications.

2. Basic Principles (PHILOSOPHY)

2.1 Components of the Internet System

Even in the Internet environment network on host and by gateway interconnected network composition.
The inter-process communication system needs the support of all levels of protocols on the host, network, and gateway, and provides bidirectional data flow on the logical connection between process ports (for example: TCP).
Each process can have several ports, and processes use ports to distinguish each other and communicate with each other.

  1. Process: An element that is considered active in the host.
  2. Inter-process communication: If terminals, files, and I/O devices communicate with each other through processes, it is also considered as inter-process communication.
  3. Host: A computer connected to a network, from the perspective of the communication network, the host is the source and destination of data packets
  4. Network: Can be a local network (eg: Ethernet ) or a large network (eg: CSTNET ), both based on packet switching technology.
  5. Packet: It is the data unit in TCP/IP protocol communication transmission, also known as "data packet". TCP doesn't care about the data structures of the upper layers.
  6. Logical connections: such as TCP connections. A physical connection, such as a network cable connection.

2.2 Operation mode

  +------------------------------------+
  |            higher-level            |
  +------------------------------------+
  |              TCP module            |
  +------------------------------------+
  |      internet protocol module      |
  +------------------------------------+
  |        network device driver       |
  +------------------------------------+
  |        communication network       |
  +------------------------------------+

TCP module -- TCP communication model:

  1. The process uses the buffer as a parameter to pass data to the TCP module.
  2. The TCP module packages the data in the cache into segment , and calls the ip module to transmit the data to the target TCP.
  3. The receiver TCP takes the data out of the segment into the receiver buffer and notifies the receiver user. Control information (TCP headers) in TCP segments is used to ensure orderly and reliable transmission of data.

internet protocol module -- IP communication model:

  1. TCP call ip module
  2. ip module encapsulates the segment into an internet datagram, and the ip header contains the ip module or the intermediate gateway.
  3. ip module encapsulates internet datagrams into local area network packets, such as MAC. Call the network card driver to send data.
  4. Packet switching can perform further packaging, fragmentation or other operations to deliver LAN to the destination ip module
  5. The gateway extracts the IP datagram from its LAN packet, checks and determines the next route. The IP datagrams are then encapsulated into LAN packets suitable for the next network and routed to the next gateway, or final destination
  6. The destination ip module expands segments and passes it to the destination TCP.

Packet switch: A communication device that applies communication technology to program data information in packets
Local Area Network: A local area network is a private network, typically within or near a building, such as a home, office, or factory.
Expand: Reassemble IP datagrams into segments

2.3 Host Environment

Suppose there is a TCP module in the operating system. User access to TCP is like access to the file system. TCP modules may call other operating system functions. It is assumed that the actual interface to the network is controlled by the device driver module. TCP calls ip module which calls network device driver .

The TCP mechanism does not exclude the front-end processor implementation. host-to-frontend protocol must support the functions of the TCP/user interface described in this document, see 2.4

Front-end processor: front end processor, FEP. Also called communication controller. Example: LVS router multiplexer.

2.4 Interface

There are two types of TCP interfaces:
TCP/user: Provided to users or applications
TCP/internet: The TCP module is provided for use by the underlying protocol (such as the IP protocol)

TCP/user: open/close connections, send/receive data, get connection status and other functions.
TCP/internet: Provides the ability to send and receive datagrams from a remote end (a TCP module of any host on the Internet). Has the following parameters: delivery address, service type, priority, security and other control information.

2.5 Relationship with other agreements

   +------+ +-----+ +-----+       +-----+
   |Telnet| | FTP | |Voice|  ...  |     |  Application Level
   +------+ +-----+ +-----+       +-----+
         |   |         |             |
        +-----+     +-----+       +-----+
        | TCP |     | RTP |  ...  |     |  Host Level
        +-----+     +-----+       +-----+
           |           |             |
        +-------------------------------+
        |    Internet Protocol & ICMP   |  Gateway Level
        +-------------------------------+
                       |
          +---------------------------+
          |   Local Network Protocol  |    Network Level
          +---------------------------+

                     Protocol Relationships

                           Figure 2.

2.6 Reliability of Communication

A stream of data sent over a TCP connection arrives at its destination in an orderly and reliable manner.

Transmissions are made reliable with serial numbers and acknowledgments. The data is assigned a serial number every 8 bits (octet). The sequence number carried in the TCP header is equal to allocated by the first 8 bits in the segment. The acknowledgment number is equal to the sequence number that the sender expects to receive next. When TCP transmits a segment containing data, it puts the copy in the retransmission queue and starts a timer; after receiving the acknowledgment number, it deletes the copy. No retransmission was received before the timer expired.

    TCP A                                          TCP B
1.  ESTABLISHED --> <SEQ=100><DATA size=1460>  --> ESTABLISHED
2.  ESTABLISHED <-- <ACK=1561><CTL=ACK><DATA>  <-- ESTABLISHED
3.  ESTABLISHED --> <SEQ=1561><DATA size=1460> --> ESTABLISHED

http://c.biancheng.net/view/2352.html

TCP acknowledgments do not guarantee that the data has been delivered to the user. Take bio as an example:

+------------------------+
|用户空间  用户空间缓冲区  |
+------------------------+
|内核空间  套接字缓冲区    |
+------------------------+
|          network       |
+------------------------+
如上图,数据到达接收方TCP,TCP检查收到的数据块和TCP头部内容,判断数据是否有丢失,没有问题返回确认。然后将数据暂存到套接字缓冲区,并将数据按顺序连接还原出原始数据,最后将数据交给应用程序。

//TODO nio是对上述流程带来的影响?

To control the data flow between TCPs, a flow control mechanism is employed. The receiver TCP reports a "window" to the sender TCP. The window represents the size in bytes that the receiver can receive.

2.7. Connection establishment and clearing

Q: How do I identify independent data streams?
Answer: A socket is a unique identifier on the network, consisting of an IP address and a port provided by tcp. A connection consists of a pair of sockets on both ends (local ip local port external ip external port). A connection can transmit in both directions and is called "full duplex". TCPs can choose any port to associate with a process.

There are some basic concepts that are required in any implementation:
socket : connection port number and program. Programs can have port numbers and initiate connections on ports.
connect : Determined by the parameters (local port, external socket) passed in when calling OPEN. TCP returns a local connection name by which the user refers to the connection in subsequent calls. An implementation strategy that sets the local connection name to a pointer to the TCB.
TCB : Transmission Control Block, used to store control information for the connection.
OPEN : The OPEN call specifies whether connection establishment is active or passively waiting.

passive OPEN : If a process wishes to serve other processes, it can use a passive OPEN request, with an external address of 0.0.0.0:0 indicating that the socket has not been specified. Unspecified sockets are only allowed in passive OPEN. The corresponding connection status is Listener .

There are two main cases for the matching rules of local passive OPENs and foreign active OPENs. First, local passive OPENs need to match exactly when specifying external sockets. Second, local passive OPENs do not specify external sockets, and any external sockets can be accepted. Other possible cases include partial strict matching.

foreign socket: 0.0.0.0:0
0表示任意,不做限制。
如果不为0,需要精确匹配。

The process of establishing a connection uses the synchronization (SYN) control flag and involves a "three-way handshake". When the sequence numbers are synchronized in both directions, the connection is established. The clearing of the connection involves the exchange of segments, which carry control flags with FIN.

2.8 Data Communication

The data stream on the connection is an 8bit stream. When the sending user calls the SEND function, the data can be sent immediately through the flag bit PUH.

The sender TCP allows to collect data from the user and then encapsulate it into a segment to send when it is convenient. After receiving the push function, all unsent data must be sent. When the receiving TCP sees the PSH flag, it immediately transfers the data to the receiving process.

TCP defines urgent data. TCP does not specify what to do when receiving urgent data. In general, the receiving process should process urgent data as soon as possible.

2.9 Security and Priorities

TCP utilizes the service fields and security options of the internet protocol to provide TCP users with per-connection priority and security.
When TCP works in a multi-level security environment, security and priority options need to be provided. Most TCP implementations are a multi-level security environment.
A TCP module operating in a multi-level security environment must correctly mark secure segments, delimited segments, and priorities. It must also provide an interface to its user or a higher-level protocol such as Telnet or THP for specifying the security level, interval, and priority of the connection.

2.10 Robustness principle

TCP implementations will follow general principles of robustness: be cautious in what you do yourself, and be liberal (fault-tolerant) in what you accept from others.
be conservative in what you do, be liberal in what you accept from others.

3. Functional Specifications

3.1 Header format

TCP segments are sent as internet datagrams. The IP header contains several message headers (source host address and destination host address). The TCP header follows the internet header.

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Source Port          |       Destination Port        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                        Sequence Number                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Acknowledgment Number                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Data |           |U|A|P|R|S|F|                               |
   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
   |       |           |G|K|H|T|N|N|                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           Checksum            |         Urgent Pointer        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             data                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Sequence Number: serial number 32 bits

这个段的第一个数据字节的序列号(出现*SYN控制位*时除外)。 如果存在SYN,序列号代表初始序列号(ISN),第一个数据字节是ISN+1。  

Acknowledgment Number: Acknowledgment number 32 bits

如果设置了*ACK控制位*,确认号=接收方期待的下一个收到的序列号。

Data Offset: data offset 4 bits

标识数据从哪里开始

Control Bits: Control bits 6 bits (from left to right):

URG:  紧急标识
ACK:  确认标识
PSH:  立即发送
RST:  重置连接
SYN:  同步序列号
FIN:  没有更多的数据需要发送了

Window: Window 16 bits

携带窗口的段的发送方愿意接受的数据字节数

Checksum: Checksum 16 bits

16字节 = 伪首部12字节,校验和4字节

首先是伪首部:源地址,目标地址,预留空间,传输层协议号(TCP是6)、TCP报文长度(报头+数据)
                     +--------+--------+--------+--------+
                     |           Source Address          |
                     +--------+--------+--------+--------+
                     |         Destination Address       |
                     +--------+--------+--------+--------+
                     |  zero  |  PTCL  |    TCP Length   |
                     +--------+--------+--------+--------+
最后计算校验和,校验和计算方式:将TCP段以16bit为单位相加,然后取反码。

Urgent Pointer: Urgent Pointer 16 bits

紧急指针指向紧急数据在报文段中结束的位置

Options: Optional options become longer//TODO is temporarily specified by 793

数据结构:
| kind(1字节) | length(1字节) | info(n字节) |

      Kind     Length    Meaning
      ----     ------    -------
       0         -       End of option list.
       1         -       No-Operation
       2         4       最大分段大小.

      End of Option List

        +--------+
        |00000000|
        +--------+
         Kind=0
表示可选项列表结束

      No-Operation

        +--------+
        |00000001|
        +--------+
         Kind=1
在可选项之间使用,占一字节,用于选项间填充数据,做对齐。

      MSS:最大分段大小
      +--------+--------+---------+--------+
      |00000010|00000100|   max seg size   |
      +--------+--------+---------+--------+
      Kind=2   Length=4
最大分段大小,只在连接初始化时使用。接收方用它来告诉发送者,我期望的最大分段大小是多少。

Padding: padding becomes longer

确保TCP头部长度为32bit的整数倍。

3.2 Terminology

Before discussing TCP operations, some terminology needs to be introduced in detail. These terms are roughly divided into 2 parts:

  1. TCB: Transmission Control Block Transmission Control Block
  2. Current segment (segment) related variables
  3. The series of states that a connection goes through during the lifetime of the connection
3.2.1 TCB

Maintaining a TCP connection requires logging some variables, which are stored in the TCB.
Variables stored in TCB include:

  1. local and remote socket numbers
  2. Connection security and priority
  3. pointers to user send and receive buffers
  4. pointer to the retransmission queue and to the current segment
  5. Several variables related to sending and receiving sequence numbers

Send serial number related variables

SND.UNA - 已发送 未经确认
SND.NXT - 发送方 下一个要发送的序列号
SND.WND - 发送窗口
SND.UP -  发送紧急指针
SND.WL1 - 上次窗口更新的时段系列号(segment sequence number used for last window update)。发送方收到接收方返回的窗口,触发窗口更新时会保存一个段序列号
SND.WL2 - 用于上次窗口更新的段确认号(segment acknowledgment number used for last Window update)。发送方收到接收方返回的窗口,触发窗口更新时会保存一个段确认号
ISS - 初始发送系列号,三次握手阶段使用

  发送序列空间,发送窗口即图4第三部分

                   1         2          3          4
              ----------|----------|----------|----------
                     SND.UNA    SND.NXT    SND.UNA
                                          +SND.WND

        1 - 已传输数据已收到确认的旧序列号
        2 - 已传输数据未收到确认数据的序列号
        3 - 当前允许用于传输数据但还未传输的序列号
        4 - 当前不允许传输的将来的系列号

                               Figure 4.

Receive serial number related variables

      RCV.NXT - 接收下一个
      RCV.WND - 接收窗口
      RCV.UP  - 接收紧急指针
      IRS     - 初始化接收序列号

  接收序列空间, 接收窗口即图5第2部分。

                       1          2          3
                   ----------|----------|----------
                          RCV.NXT    RCV.NXT
                                    +RCV.WND

        1 - 已确认的旧序列号
        2 - 允许接收的新序列号
        3 - 不允许接收的未来序列号
        
                               Figure 5.
    
3.2.2 Current segment (segment) related variables

There are also variables frequently used in the discussion whose values are derived from the variables of the current section.

Current segment variable

      SEG.SEQ - 段序列号
      SEG.ACK - 段确认号
      SEG.LEN - 段长度
      SEG.WND - 段窗口
      SEG.UP  - 段紧急指针
      SEG.PRC - 段优先级字段
3.2.3 The series of states that a connection goes through during the life cycle

A connection goes through a series of states during its life cycle:

建立连接
LISTEN、SYN-SENT、SYN-RECEIVED、
连接中
ESTABLISHED
断开连接
FIN-WAIT-1、FIN-WAIT-2、CLOSE-WAIT、CLOSING、LAST-ACK、TIME-WAIT
不在连接状态
CLOSED

CLOSED is fictitious and means that there is no connection state and TCB does not exist.

State Machine :


                              +---------+ ---------\      active OPEN
                              |  CLOSED |            \    -----------
                              +---------+<---------\   \   create TCB
                                |     ^              \   \  snd SYN
                   passive OPEN |     |   CLOSE        \   \
                   ------------ |     | ----------       \   \
                    create TCB  |     | delete TCB         \   \
                                V     |                      \   \
                              +---------+            CLOSE    |    \
                              |  LISTEN |          ---------- |     |
                              +---------+          delete TCB |     |
                   rcv SYN      |     |     SEND              |     |
                  -----------   |     |    -------            |     V
 +---------+      snd SYN,ACK  /       \   snd SYN          +---------+
 |         |<-----------------           ------------------>|         |
 |   SYN   |                    rcv SYN                     |   SYN   |
 |   RCVD  |<-----------------------------------------------|   SENT  |
 |         |                    snd ACK                     |         |
 |         |------------------           -------------------|         |
 +---------+   rcv ACK of SYN  \       /  rcv SYN,ACK       +---------+
   |           --------------   |     |   -----------
   |                  x         |     |     snd ACK
   |                            V     V
   |  CLOSE                   +---------+
   | -------                  |  ESTAB  |
   | snd FIN                  +---------+
   |                   CLOSE    |     |    rcv FIN
   V                  -------   |     |    -------
 +---------+          snd FIN  /       \   snd ACK          +---------+
 |  FIN    |<-----------------           ------------------>|  CLOSE  |
 | WAIT-1  |------------------                              |   WAIT  |
 +---------+          rcv FIN  \                            +---------+
   | rcv ACK of FIN   -------   |                            CLOSE  |
   | --------------   snd ACK   |                           ------- |
   V        x                   V                           snd FIN V
 +---------+                  +---------+                   +---------+
 |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
 +---------+                  +---------+                   +---------+
   |                rcv ACK of FIN |                 rcv ACK of FIN |
   |  rcv FIN       -------------- |    Timeout=2MSL -------------- |
   |  -------              x       V    ------------        x       V
    \ snd ACK                 +---------+delete TCB         +---------+
     ------------------------>|TIME WAIT|------------------>| CLOSED  |
                              +---------+                   +---------+

                      TCP Connection State Diagram
                               Figure 6.

3.3 Serial number

There is a basic concept when designing TCP: every 8 bits of data on a TCP connection is assigned a sequence number.
Since the data stream is assigned a sequence number every eight bits, no sequence number can be used for confirmation, and the confirmation number X means that the sequence numbers before X have been confirmed, but X is not included. This mechanism can be used for retransmission. The first 8 bits immediately after the header are the lowest sequence numbers, and the next 8 bits are consecutive sequence numbers.

The serial number is of int type, and the value range is 0 ~ 2^32-1(4,294,967,296) == 4G. For the overflowed value, the computer performs the modulo operation.

Several types of sequence number comparisons that a typical TCP must handle:

(a) 确认(acknowledgements)指向的序列号已发送但未确认
(b) 段中的序列号已确认,从重传队列中移除段
(c) 接收到的段中包含符合预期的序列号(是否在接收窗口范围内)。

In response to sending data, when TCP receives acknowledgements, the following comparisons are made:

SND.UNA = 最大未确认的序列号
SND.NXT = 下一个要发送的序列号
SEG.ACK = 接收方TCP发送过来的确认 (接收TCP所期望的下一个序列号)
SEG.SEQ = 段的第一个序列号
SEG.LEN = 段中数据占用的字节数(包括SYN和FIN)
SEG.SEQ+SEG.LEN-1 = 段的最后序列号

一个新的确认号,遵循下方的不等式:
SND.UNA < SEG.ACK =< SND.NXT
如果重传队列上的段的序列号和长度之和小于或等于传入段中的确认值,则该段被完全确认

The following comparisons need to be made when receiving data:

RCV.NXT = 预期接收到的下一个段的序列号,在接收窗口的左边缘  RCV.NXT+RCV.WND-1 = 预期接收段的最后一个段,再接收窗口的右边缘
SEG.SEQ = 接收段的占用的第一个序列号
SEG.SEQ+SEG.LEN-1 = 接收段占用的最后一个序列号

检查段序列号的合法性性,需满足以下两个条件:
1. RCV.NXT =<SEG.SEQ<RCV.NXT+RCV.WND
2. RCV.NXT=<SEG.SEQ+SEG.LEN-1<RCV.NXT+RCV.WND

实际情况需要考虑,零窗口和0长度段的存在,段的接收有4种情况:

    Segment Receive  Test
    Length  Window
    ------- -------  -------------------------------------------

       0       0     SEG.SEQ = RCV.NXT

       0      >0     RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND

      >0       0     not acceptable

      >0      >0     RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
                  or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND

Note that when the receive window is 0, only the ACK segment is received. Therefore, it is possible for TCP to maintain a zero receive window when transmitting data and receiving ACKs. However, even if the receive window is 0, TCP MUST process the RST and URG fields of the segment.

3.3.1 ISN (Initial Serial Number)

// TODO Why is ISN a random number and its random algorithm?

Each segment sent will occupy one or more sequence numbers in the sequence space, and the numbers occupied by the segment are in the "busy" or "in use" state until MSL seconds (maximun segment lifetime).

3.3.2 TCP Quiet Time Concept

3.4 Establishing a connection

  TCP A                                                TCP B
1.  CLOSED                                               LISTEN
2.  SYN-SENT    --> <SEQ=100><CTL=SYN>               --> SYN-RECEIVED
3.  ESTABLISHED <-- <SEQ=300><ACK=101><CTL=SYN,ACK>  <-- SYN-RECEIVED
4.  ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK>       --> ESTABLISHED
5.  ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK><DATA> --> ESTABLISHED

                      三次握手
                    Figure 7.

  TCP A                                            TCP B
1.  CLOSED                                           CLOSED
2.  SYN-SENT     --> <SEQ=100><CTL=SYN>              ...
3.  SYN-RECEIVED <-- <SEQ=300><CTL=SYN>              <-- SYN-SENT
4.               ... <SEQ=100><CTL=SYN>              --> SYN-RECEIVED
5.  SYN-RECEIVED --> <SEQ=100><ACK=301><CTL=SYN,ACK> ...
6.  ESTABLISHED  <-- <SEQ=300><ACK=101><CTL=SYN,ACK> <-- SYN-RECEIVED
7.               ... <SEQ=101><ACK=301><CTL=ACK>     --> ESTABLISHED

                    同时发起连接
                      Figure 8.
  TCP A                                                TCP B
1.  CLOSED                                               LISTEN
2.  SYN-SENT    --> <SEQ=100><CTL=SYN>               ...
3.  (duplicate) ... <SEQ=90><CTL=SYN>               --> SYN-RECEIVED
4.  SYN-SENT    <-- <SEQ=300><ACK=91><CTL=SYN,ACK>  <-- SYN-RECEIVED
5.  SYN-SENT    --> <SEQ=91><CTL=RST>               --> LISTEN
6.              ... <SEQ=100><CTL=SYN>               --> SYN-RECEIVED
7.  SYN-SENT    <-- <SEQ=400><ACK=101><CTL=SYN,ACK>  <-- SYN-RECEIVED
8.  ESTABLISHED --> <SEQ=101><ACK=401><CTL=ACK>      --> ESTABLISHED
                    Recovery from Old Duplicate SYN
                               Figure 9.
  TCP A                                           TCP B
1.  (CRASH)                               (send 300,receive 100)
2.  CLOSED                                           ESTABLISHED
3.  SYN-SENT --> <SEQ=400><CTL=SYN>              --> (??)
4.  (!!)     <-- <SEQ=300><ACK=100><CTL=ACK>     <-- ESTABLISHED
5.  SYN-SENT --> <SEQ=100><CTL=RST>              --> (Abort!!)
6.  SYN-SENT                                         CLOSED
7.  SYN-SENT --> <SEQ=400><CTL=SYN>              -->
                     半连接
                     Figure 10.

// TODO semi-join with RST


wzyAcyy
9 声望2 粉丝