Computer Networking - UDP and TCP Protocols in the Transport Layer

Since the UDP and TCP protocols are the most important content in the transport layer, I will write a separate article here to help everyone (myself) understand better.

1. UDP protocol

UDP only adds two basic services to the datagram service of IP: multiplexing and demultiplexing and error detection.

Advantages of using UDP:

  1. UDP does not need to establish a connection

  2. no connection state

  3. Packet header overhead is small

  4. The application layer has more control over what data to send and when

  5. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communications.

1.1 UDP header format

A UDP datagram consists of two parts: UDP header and user data. The UDP header has 8B and consists of four fields, and the length of each field is 2B. They are:

  1. source port

  2. destination port

  3. length

  4. checksum

When the transport layer receives the UDP datagram from the IP layer, it sends the UDP datagram to the application process through the corresponding port according to the destination port in the header. If the receiver UDP finds that the destination port number in the received message is incorrect, it discards the message, and ICMP sends a "port unreachable" error message to the sender.

1.2 UDP inspection

When calculating the checksum, a 12B pseudo-header should be added before the UDP datagram. The dummy header is neither sent down nor up, but only for the purpose of calculating the checksum. The UDO's checksum checks the header and data sections.

Calculation method of UDP checksum:

The sender first puts all 0s into the checksum field and adds a header, then fills in a byte of all 0s at the end of the data part (but this byte is not sent). Then calculate the sum of these 16 words according to the binary complement code, write the binary complement code of this sum into the checksum field and send it. The receiver adds a dummy header to the received UDP datagram, writes the one's complement of the sum into the verification amount field and sends it. The receiver adds the pseudo-header to the received UDP datagram and then adds the binary one's complement code to find the sum of these 16-bit words. When there is no error, the result should be all 1, otherwise it indicates that there is an error, and the receiver should discard this UDP datagram.

2. TCP protocol

The TCP protocol is a reliable data transmission protocol implemented on the unreliable IP layer. It mainly solves the problems of reliable, orderly, loss-free and non-repetitive transmission. TCP is a very complex protocol in the TCP/IP system. Its main features are as follows:

  1. TCP is a connection-oriented transport layer protocol, and a TCP connection is a logical connection.

  2. Each TCP connection can only have two endpoints, and each TCP connection can only be point-to-point (one-to-one)

  3. TCP provides reliable delivery services, ensuring that the transmitted data is error-free, not lost, not repeated and in order.

  4. TCP provides full-duplex communication, allowing the application processes of both parties to communicate to send data at any time. For this reason, both ends of the TCP connection are equipped with a sending cache and a receiving cache to temporarily store data for two-way communication.

  5. TCP is byte stream-oriented. Although the interaction between the application program and TCP is one data block (of different sizes) at a time, TCP only regards the data handed over by the application program as a series of unstructured byte streams.

TCP and UDP use completely different methods when sending packets. The length of the UDP message is determined by the sending application process, while the length of the TCP message is determined according to the window value given by the receiver and the current network congestion level. If the data block sent by the application to the TCP cache is too long, TCP will divide it into shorter pieces before sending it; if it is too short, TCP can also wait until enough bytes have been accumulated before forming a segment and sending it out.

2.1 TCP segment

A unit of data transmitted by TCP is called a segment. The TCP segment can be used not only to carry data, but also to establish a connection, release a connection and reply.

A TCP segment is divided into two parts: header and data. The entire TCP segment is encapsulated in the IP datagram as the data part of the IP datagram. The first 20B of its header is fixed. The shortest TCP header is 20B, and the following 4N bytes are options that can be added according to needs.

[Learning address]: FFmpeg/WebRTC/RTMP/NDK/Android audio and video streaming media advanced development

[Article Benefits]: Receive more audio and video learning packages, Dachang interview questions, technical videos and learning roadmaps for free. The materials include (C/C++, Linux, FFmpeg webRTC rtmp hls rtsp ffplay srs, etc.) Click 1079654574 to join the group to receive it~

The fields in the header are:

  1. The source port and the destination port each occupy 2B. The port is the service interface between the transport layer and the application layer, and the multiplexing and demultiplexing functions of the transport layer must be realized through the port.

  2. The serial number occupies 4B. Each byte in the byte stream transmitted by a TCP connection is numbered sequentially. The value of the sequence number field refers to the sequence number of the first byte of the data sent in this segment.

  3. Confirmation number, 4B. It is the sequence number of the first data byte of the next segment expected to be received by the other party. If the confirmation number is N, it means that all the data up to sequence number N-1 have been received correctly.

  4. Data offset (that is, the length of the header), which occupies 4 bits. It indicates how far the beginning of the data of the TCP segment is from the beginning of the TCP segment. The unit of "data offset" is 32 bits (calculated in 4B). Since the maximum value that a 4-bit binary number can represent is 15, the maximum length of the TCP header is 60B.

  5. Reserved, occupying 6 places.

  6. Emergency bit URG. When URG=1, it indicates that the urgent pointer field is valid, indicating that there is urgent data in this segment and should be transmitted as soon as possible.

  7. Confirmation bit ACK, when ACK=1, the confirmation number field is valid. TCP stipulates that after the connection is established, all transmitted segments must set ACK to 1.

  8. Push bit PSH (push). When the receiving TCP receives the segment with PSH=1, it will deliver it to the receiving application process as soon as possible, instead of waiting until the entire cache is full before delivering it upwards.

  9. Reset bit RST (reset). When RST=1, it indicates that there is a serious error in the TCP connection, the connection must be released, and then the transport connection must be re-established.

  10. Synchronization bit SYN. When SYN=1, it means that this is a connection request or connection acceptance message.

  11. Termination bit FIN (finish). When FIN=1, it indicates that the data of the sender of this segment has been sent, and the transport connection is required to be released.

  12. The window occupies 2B. Indicates the amount of data that is allowed to be sent by the other party.

  13. Checksum, accounting for 2B. The scope of the inspection includes two parts, the header and the data.

  14. Urgent pointer, occupying 2B. It is meaningful when URG=1, indicating how many bytes of urgent data there are.

  15. option, variable length. MSS is the maximum length of the data field in a TCP segment.

  16. Padding, so that the length of the entire header is an integer multiple of 4B.

2.2 TCP connection management

The port of the TCP connection is socket, and every day the TCP connection is uniquely determined by the two endpoints of communication (that is, two sockets).

The establishment of the TCP connection adopts the client/server mode. The application process that actively initiates the connection establishment is called the client, while the application process that passively waits for the connection establishment is called the server.

2.2.1 Establishment of TCP connection

  1. The client's TCP first sends a connection request segment to the server. At this time, the synchronization bit SYN in the header is set to 1, and an initial sequence number seq=x is selected at the same time. TCP stipulates that the SYN segment cannot carry data, but consumes a sequence number.

  2. After the server's TCP receives the connection request segment, if it agrees to establish a connection, it sends back an acknowledgment to the client, and allocates buffers and variables for the TCP connection. In the confirmation message segment, set the SYN and ACK bits to 1, the confirmation number is ack=x+1, and at the same time choose an initial sequence number seq=y for yourself. The acknowledgment segment cannot carry data, but consumes a sequence number.

  3. After the client computer receives the confirmation message segment, it also needs to give confirmation to the server, and allocate buffers and variables for the TCP connection. The ACK position of the confirmation message segment is 1, the confirmation number ack=y+1, and the sequence number seq=x+1. This segment can carry data, and if it does not carry data, the sequence number will not be consumed.

After successfully performing the above three steps, a TCP connection is established. It is worth noting that resources on the server side are allocated when the second handshake is completed, while resources on the client side are allocated when the third handshake is completed, which makes the server vulnerable to SYN flood attacks.

(TCP release will not be described here.)

3. Summary:

  1. connection established

    • SYN=1,seq=x

    • SYN=1,ACK=1,seq=y,ack=x+1

    • ACK=1,seq=x+1,ack=y+1

  2. release connection

    • FIN=1,seq=u

    • ACK=1,seq=v,ack=u+1

    • FIN=1,ACK=1,seq=w,ack=u+1

    • ACK=1,seq=u+1,ack=w+1

Due to space reasons, it will be recorded here for the time being~

Original link: Computer Network - UDP and TCP Protocols in the Transport Layer - Nuggets

Guess you like

Origin blog.csdn.net/irainsa/article/details/130414303