Hello everyone!
Today, we will continue to learn about TCP/IP.
Transport layer
The transport layer shields network complexity for upper-layer applications and implements end-to-end connectivity between host applications. The transport layer provides the following basic functions:
(1) Segments the data sent from the application layer to the network layer or combines the data sent from the network layer to the application layer, that is, encapsulates and decapsulates the data.
(2) Establishing end-to-end connections is mainly to establish logical connections to transmit data flows.
(3) Send the data segment from one host to another. During transmission, checksums are calculated and flow control is performed. This method ensures data correctness and prevents buffer overflow.
(4) Some transport-layer protocols ensure correct data transmission. During data transmission, ensure that the same data is not transmitted multiple times and is not lost, and ensure that the sequence of receiving data packets is the same as that of sending data packets.
Transport Layer Port Number

There are two types of transport layer protocols in the TCP/IP protocol stack: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP).

Although both TCP and UDP use IP as their network layer protocol, they provide distinct services for the application layer.
TCP provides connection-oriented and reliable byte stream services. Connection-oriented means that a TCP connection must be established between two applications that use TCP as the transport layer protocol before they exchange data. TCP provides reliable transmission services for upper-layer applications through mechanisms such as confirmation, verification, and reassembly. However, the establishment, validation, and verification of TCP connections consume a large amount of work and bring a large amount of overhead.
UDP provides simple and datagram-oriented services. UDP does not guarantee the reliability, that is, packets cannot reach the destination. UDP is applicable to applications that focus more on transmission efficiencies, such as SNMP and RADIUS. SNMP monitors networks and intermittently sends alarms. If a TCP connection needs to be set up each time a small amount of information is sent, the transmission efficiency will be reduced. Therefore, applications that focus more on transmission efficiencies, such as SNMP and RADIUS, select UDP as the transport layer protocol. In addition, UDP is also applicable to application-layer protocols that have reliability mechanisms.
Application layer
The application layer is the highest layer in the system architecture. It directly provides services for user application processes. The application layer has the following functions:
Provides interfaces for users to process-specific applications.
Data encryption, decryption, compression, and decomposition.
The standard for Defining Data Representation.

There are many protocols in the application layer. The following protocols help you use and manage the TCP/IP network:
FTP (File Transfer Protocol): Used to transfer independent files, usually for interactive user sessions.
HTTP (Hypertext Transfer Protocol): Used to transfer the files that make up the pages on the World Wide Web.
TELNET: Transfers data with Telnet control information. It provides a standard method for interacting with terminal devices or terminal processes and supports terminal-to-terminal connections and process-to-process distributed computing.
SMTP (Simple Message Transfer Protocol) & POP3(Post Office Protocol).
DNS (Domain Name Server): Domain names can be converted to IP addresses, and domain name resources can be managed in a distributed manner.
TFTP (Trivial File Transfer Protocol): File transfer design for general purpose, high throughput.
RIP (Routing Information Protocol): A protocol used by a router to exchange routing information on an IP network.
SNMP (Simple Network Management Protocol): Collects network management information and exchanges network management information between the network management console and network devices (such as routers, bridges, and servers).
Radius (Remote Authentication Dial-In User Service): Remote authentication protocol for dial-up access, which implements authentication, authorization, and accounting for access users.
Data Encapsulation and De-encapsulation in TCP/IP Model
In the OSI and TCP/IP layered model, the functionality of each layer is briefly described. The data sending process is similar to the mailing process. When an item is mailed, the item is encapsulated into a package and the sender and recipient information is filled in. The data to be sent needs to be encapsulated with a header packet containing information such as the IP address and MAC address.

In the TCP/IP layer, the data exchanged between the peer layers is called the protocol data unit (PDU). PDUs have common names at different layers. For example, at the transport layer, the PDU obtained after the TCP header of the upper layer is called a segment. The data segment is transmitted to the network layer. The PDU obtained by adding an IP header is considered as a packet. The data packet is transmitted to the data link layer. The PDU obtained by encapsulating the header of the data link layer is called a frame. Finally, the frame is converted into bits and transmitted over the network medium.
Assume that two hosts A and B communicate with each other. Host A converts an application into upper-layer data using an upper-layer protocol and sends the data to the transport layer. The transport layer uses the PDU at the application layer as its data part and encapsulates the header at the transport layer. If the packet is large, the transport layer segments the PDU to form the PDU at the transport layer and transmits the PDU to the network layer. The network layer uses the PDU at the transport layer as the data part of the data layer and adds the header information of the network layer to form the PDU at the network layer and transmits the PDU to the data link layer. The data link layer adds a data link layer header to the header to form a data frame and then transmits the data frame to the physical layer. The physical layer converts the data into bitstreams and transmits the bitstreams to host B through physical lines. This protocol stack passes data down and adds headers and tails to the packets is called encapsulation.
After receiving the bitstream at the physical layer, host B sends the bitstream to the data link layer for processing. After receiving the packet, the data link layer removes the header of the data link layer packet and transmits the packet to the network layer.
After receiving the packet, the network layer removes the IP packet header from the packet and sends the packet to the transport layer for processing. The transport layer removes the IP packet header from the packet and sends the packet to the application layer.
It should be noted that the data link layer is represented as two logical sublayers: LLC and MAC. In actual applications, only the header information of the MAC sublayer needs to be encapsulated according to different protocols. The frame check sequence is used to check whether there is a transmission error during data transmission. It is an error check mechanism.
After data is transmitted over the network and reaches the receiving device, the receiving device deletes the added information and determines how to upload the data to the proper application program along with the protocol stack based on the information in the header. This process is called decapsulation.
Data encapsulation and de-encapsulation are performed layer by layer. Each layer processes the data of the upper layer or lower layer and adds or removes the header of the encapsulated packet.
The Encapsulation Process of TCP/IP Protocol Stack

The preceding figure shows the encapsulation process of TCP/IP packets when TCP is used at the transport layer, IP is used at the network layer, and Ethernet is used at the link layer. The figure shows the length of each field in the Ethernet data encapsulation. The following describes the encapsulation of each layer from top to bottom.
TCP Segment

The TCP data segment is encapsulated in the IP data packet. A TCP data packet consists of a TCP header and a TCP data packet. The TCP header contains a maximum of 60 bytes. If the Options field is unavailable, the normal length is 20 bytes.
The following figure shows the TCP header. For details about the functions of the TCP header, see the transport layer protocol.
Source Port: Indicates the source port number. TCP assigns a source port number to the application.
Destination Port: Destination port number.
Sequence Number: Identifies the data byte stream sent from the TCP sender to the TCP receiver.
Ack Num: The acknowledgment sequence number contains the next sequence number expected by the sending end. Ensure that the sequence number is the sequence number of the data successfully received last time plus 1.
Option: The option field
IP Packet

After receiving the TCP data segment from the transport layer, the network layer adds the network layer IP header information to the segment. The fixed length of a common IP header is 20 bytes (excluding the IP option field). An IP packet header consists of the following fields:
Version indicates the IP protocol version. Currently, the IP protocol version is 4. The version number of the next-generation IP protocol is 6.
The packet length refers to the number of 32-bit words occupied by the header, including any options. It is a 4-bit field, 2^4=16. There are 15 valid value fields except for all-0 items. The maximum value is 15, indicating that the header occupies 15 32-bit bits. Therefore, 32 x 15/8 = 60 bytes and the maximum length of the header is 60 bytes.
The 8-bit Type of Service (TOS) field includes a 3-bit Class of Service (COS) field, a 4-bit TOS field, and a 1-bit unused bit. The 4-bit ToS represents the minimum delay, maximum throughput, maximum reliability, and minimum cost.
Total length is the length of the entire IP datagram, including the data part. This field is 16 bits long. Therefore, the maximum length of an IP datagram can reach 65535 bytes. Although an IP datagram of up to 65,535 bytes can be transmitted, it is fragmented at most link layers. In addition, the host cannot receive data packets larger than 576 bytes. UDP restricts the length of a user data packet to 512 bytes, which is less than 576 bytes. In fact, most implementations today, especially those that support NFS, allow IP datagrams larger than 8192 bytes
The Identification field uniquely identifies each packet sent by the host. Generally, the value increases by 1 each time a packet is sent.
The Time to Live (TTL) field sets the number of routers that a packet can pass through. Once a packet passes through a router, the TTL value decreases by 1. When the TTL value is 0, the packet is discarded.
The Protocol field identifies the upper-layer protocol carried in the data packet. Similar to the port number, the IP protocol uses the protocol number to identify the upper-layer protocol. The protocol number of TCP is 6, and that of UDP is 17.
The IP header checksum field is used to calculate the checksum of the IP header and check the integrity of the packet header.
The source IP address and destination IP address fields identify the IP addresses of the source and destination devices in a data packet.
Issue of IP Fragment
At the physical network layer, the maximum length of frames to be sent each time is limited. When the IP layer receives an IP packet to be sent, it needs to determine the local interface to which the packet is to be sent (route selection) and query the interface to obtain the MTU. The IP compares the MTU with the data packet length and fragments the data packet if necessary.

Fragment Principle: The IP layer compares the data length with the MTU. If the data length is greater than the MTU, the IP layer fragments the data.
The fragment can occur either on the original sender host or on an intermediate router.
After an IP datagram is fragmented, it is reassembled only when it reaches the destination.
The entire data packet is retransmitted even if a piece of data is lost.
Any transport layer header appears only in the first piece of data.
Ethernet Frame

The Ethernet header consists of three fields:
DMAC: indicates the MAC address of the destination terminal.
SMAC: indicates the source MAC address.
LENGTH/TYPE field: the meaning varies according to the value:
When LENGTH/TYPE > 1500, the data frame type (such as the upper-layer protocol type) is represented by the following protocol types:
− 0X0800 IP packet
− 0X0806 ARP request/response packet
− 0X8035 RARP request/response packet
When LENGTH/TYPE < 1500, it indicates the length of the data frame.
DATA/PAD: Indicates specific data. According to the Ethernet, the minimum length of the data part is 46 bytes, but the length is less than 46 bytes. Pad bytes need to be added to the data part.
FCS field: frame check sequence field, which is used to determine whether an error occurs in a data frame.
Thanks for reading!