This paper is participating in theNetwork protocols must be known and must be known”Essay campaign

Why network layering

The five-tier/seven-tier network architecture model is a platilly discussed problem. The purpose of layering is to make each layer independently responsible for their own work and realize the standardization of protocols at each layer. In the layered network architecture, data is transmitted from the upper layer to the lower layer. Each layer adds a protocol header to the packet and then transmits the packet to the lower layer. When receiving data, the data is transmitted from the lower layer to the upper layer. Each layer parses the protocol header of the packet and then transmits the data to the upper layer. As shown in the figure below

This paper mainly describes the TRANSMISSION layer OF TCP protocol, from the TCP header, with popular language to clear TCP protocol.

View THE TCP protocol from the TCP header

Three-way handshake

The three-way handshake ensures data reliability and is an important feature of THE TCP protocol

  1. A says to B: My serial number is X. Are you ready?
  2. B says to A: I have received your X serial number message. My serial number is Y. Are you ready?
  3. A says to B: I have received your Y serial number message. My serial number is X +1

According to the description above, we need two flag bits

  1. SYN (Synchronize) is a packet sent to synchronize connection requirements. It is used for “Are you ready?” .
  2. ACK (acknowledge) refers to an acknowledgement message, that is, I acknowledge receiving your message.

We need two more values

  1. Sequence number (SEQ) is the sequence number of a packet, that is, my sequence number is X. Each packet should have a sequence number to indicate the sequence.
  2. Acknowledge number (ACK) indicates the acknowledge number of a message, that is, “I acknowledge receiving your message with the X serial number.” If a message with the X serial number is received, the acknowledge number is X +1.

In this way, we can express the specific message of the three-way handshake as

  1. A sends SYN=1, seq=x to B
  2. B sends SYN=1, SEq =y, ACK=1, ACK= x+1 to A
  3. A sends seq=x+1, ACK=1, ACK= y+1 to B

The TCP header, as shown below, contains the flag bits (SYN, ACK) and two values (sequence number and acknowledgement number) needed in a three-way handshake.

The format of the TCP header is shown in the three-way handshake. In this section, we take a look at the TCP protocol based on the TCP header.

How is data passed to different processes

Each process occupies a separate port number, so once you know the port, you know which application the data should be passed to. The source port and destination port are stored in the TCP header to know where the data is coming from and where it is going. After receiving the packet, the receiver switches the source port and destination port and sends back the data.

How do I send and receive byte streams

TCP is a protocol based on byte streams. The data sent and received are byte streams. TCP transfers data through the sending and receiving Windows. The size of these two Windows is not fixed, so they are called sliding Windows, which can ensure that the receiver can receive in time under various network conditions. How the receiver tells the sender the size of the window is controlled by the “window” section of the TCP header.

In the following figure, the receiver tells the sender that the current window value is 20 bytes, then the sending window is set up from the last byte received confirmation (i.e., 30) to the sending window [31,50], in which bytes can be sent; After receiving a sequence of bytes, the receiver acknowledges the last byte received in sequence.

As you can see from the TCP header, the size of this window value is expressed in 2 bytes, up to 64KB, which is obviously not enough. So TCP later introduced a scaling factor to convert the window value size to a multiplier. This value is stored as an extension field in options in the TCP header and is represented by the Type, length, value triplet, with the window scale value having a type value of 3.

Four times to wave

  1. A says to B: My serial number is X, I want to close the connection
  2. User B says to User A, “I have received your SERIAL number X. My serial number is Y.
  3. B sends the remaining data to A
  4. B says to A: I have received your SERIAL number X. My serial number is Z. I want to close the link
  5. A says to B: I have received your Z serial number message

As described above, a flag bit FIN is required in addition to ACK

FIN (Finish) : Tell the other party that I want to close the connection and stop sending data.

In this way, we can express the specific message of four waves as

  1. A sends the message seq=x, FIN=1 to B
  2. B sends seq=y, ACK=1, ACK= x+1 to A
  3. B sends the remaining data to A
  4. B sends the following message to A: SEq = Z, ACK=1, ACK= x+1, FIN=1
  5. A sends seq=x+1, ACK=1, ACK= z+1 to B

^C Interrupt the request

Control + C is our usual interrupt method. For TCP based on byte streams, the receiver needs to know which data is urgent, like this interrupt request, which needs to be processed urgently. To achieve this, the receiver needs to know that the message “Does it contain urgent data? “And” What is the emergency data?” This requires an identifier bit and a value indicating the data range, which is the URG identifier bit and the emergency pointer field we see in the TCP header

URG (Urgent) : Indicates that the packet contains urgent data

Urgent pointer: indicates the cutoff bit of urgent data in packets, whose starting bit starts from the serial number SEQ, so the range of urgent data is [Seq, Urgent Pointer]

The following quote is from RFC1122

the urgent pointer points to the sequence number of the LAST octet (not LAST+1) in a sequence of urgent data

Why can TCP-based SSH use short commands?

To efficiently send and receive data, TCP has buffers on the requesting side and sending side for data storage, as shown in the following figure

For short commands (such as SSH login), how to ensure that the buffer is bypasses at the transport layer, the sending side immediately sends the packet to the downstream for processing, and the receiving side immediately returns the data to the upper layer?

The answer is the PUSH flag bit in the TCP header.

When the PUSH flag bit is set to 1, packet data is processed immediately instead of waiting for other data to enter the buffer. Note that this flag bit is usually not set at the application layer, but is set by TCP when the TCP layer clears the cache and sends the segment to the IP layer.