[Advanced Network] Posix API and Network Protocol Stack (4)

1. UDP data frame

User Datagram Protocol (UDP) is a connectionless network protocol that provides simple, unreliable datagram transmission services at the transport layer. Because UDP lacks complex features such as timeout retransmission, out-of-order data processing, congestion control, etc., it has lower latency and overhead compared to TCP. This makes UDP the first choice for real-time applications (such as VoIP, games) and simple query applications (such as DNS).

The UDP data frame structure is relatively simple and includes the following parts:

  • Source Port: This is an optional field that identifies the port of the application sending UDP datagrams.
  • Destination Port: This field is used to identify the port of the application that receives UDP datagrams.
  • Length: This field defines the total length of the UDP header and data.
  • Checksum: This is an optional field used to check header and data errors.

Of course, I will restate the description to illustrate the entire UDP data frame creation and transmission process as clearly as possible.

1.1 The transmission process of UDP data frame in the network protocol stack

Suppose an application wants to send a copy of data to an application on another machine. This process can be broken down into the following steps:

  1. Application Layer : The application at the sending end prepares the data and decides to use the UDP protocol for transmission. This data is called raw data or payload.

  2. Transport Layer : The application layer delivers data to the UDP protocol of the transport layer. The UDP protocol will add a UDP header, which contains the source port and destination port, as well as length and checksum fields. This process forms a UDP datagram.

  3. Network Layer : The UDP protocol of the transport layer delivers UDP datagrams to the IP protocol of the network layer. The IP protocol will add an IP header, including source IP address and destination IP address. This process forms an IP datagram, which contains UDP datagrams.

  4. Link Layer : The network layer hands IP datagrams to the link layer. The link layer will add the header and trailer of the link layer before and after the IP datagram to form a data frame. This data frame will then be sent over the physical medium (such as Ethernet, wireless network).

When the data frame reaches the receiving machine, the process is reversed:

  1. Link Layer : The link layer at the receiving end receives the data frame from the physical medium, removes the header and trailer, and then hands the IP datagram to the network layer.

  2. Network Layer : The network layer receives the IP datagram, removes the IP header, and then hands the UDP datagram to the transport layer.

  3. Transport Layer : The transport layer receives the UDP datagram, removes the UDP header, and then hands the original data or payload to the application layer.

  4. Application Layer : Finally, the application layer receives the original data and then performs subsequent processing.

The above is the transmission process of UDP data frame in the network protocol stack. It can be seen that during the transmission process, the original data will be encapsulated into data frames layer by layer, and then stripped out layer by layer at the receiving end. Each layer adds its own header information to the data to achieve its respective functions. The role of UDP is to provide end-to-end data transmission services at the transport layer.

1.2 Characteristics of UDP data frames

  1. Connectionless : UDP is a connectionless protocol, meaning there is no need to establish a connection before sending data. This makes UDP particularly suitable for one-time data transfers, such as DNS queries or video streaming.

  2. Unreliability : UDP is an unreliable protocol. It does not guarantee that data frames will successfully reach their destination. If the network is congested, UDP data frames may be dropped. Additionally, UDP does not reorder data frames if they arrive in the wrong order.

  3. Suitable for real-time applications : Because UDP does not require acknowledgment information, retransmission mechanisms or complex link establishment processes, it has extremely low latency. This makes UDP extremely valuable in real-time applications such as VoIP calls, online games, etc. Although there may be some amount of data loss, for these applications, the increase in latency will have a greater impact on the user experience.

  4. Support broadcast and multicast : UDP supports broadcast and multicast. Broadcast allows a UDP data frame to be sent to all hosts on the network, while multicast allows a data frame to be sent to a specific host group. This makes UDP ideal for distributing information over a network, such as streaming live video over a network.

2. Ethernet protocol header

The Ethernet protocol header is part of the data link layer and is used to specify the source and destination addresses of the Ethernet frame and define the frame type.

2.1 Main components of the Ethernet protocol header

  1. Destination Address : This field is a 6-byte field that contains the physical address of the destination Ethernet interface. If a machine wants to send a data frame, it will write the destination machine's Ethernet address into this field.

  2. Source Address : This field is also 6 bytes and contains the physical address of the sending machine's Ethernet interface. In this way, the receiving end can know where the data frame comes from.

  3. Type/Length : This is a 2-byte field. If the value of this field is greater than or equal to 1536 (0x0600), then it represents the type of Ethernet frame; if the value of this field is less than or equal to 1500, then it represents the length of the Ethernet frame. The type field is used to identify the upper layer protocol type, such as IPv4 or IPv6.

  4. Virtual LAN Tag (VLAN Tag, optional) : This is an optional 4-byte field that contains the Priority Code Point (PCP), Frame Identification Protocol Identifier (Drop Eligible Indicator, DEI) and virtual LAN identifier (VLAN Identifier, VID). This field does not exist in all Ethernet frames and only appears when the 802.1Q VLAN tag exists.

Please note that the above fields describe the header of the Ethernet frame. The actual Ethernet frame also includes the data part and the Frame Check Sequence (FCS) field. The FCS field is used to check the integrity of the Ethernet frame. Verification to detect whether errors occur during data transmission.

The design of the Ethernet frame header enables network equipment to accurately send data frames to the correct destination, while also providing a certain guarantee for data integrity. In the actual network environment, the Ethernet protocol is one of the most widely used LAN technologies at present. Its design ideas and technical details are worthy of our in-depth study and understanding.

2.2 Design ideas and technical details of the Ethernet protocol

  1. Simplicity : One of the design philosophies of Ethernet is to keep it simple. For example, Ethernet is a broadcast-based protocol, and all nodes are in the same broadcast domain. This means that each node will receive all data frames and then decide whether to process the data frame based on the destination address in the Ethernet frame header.

  2. Connectionless : Ethernet is connectionless, that is, the node sending the data frame does not need to establish a connection with the receiving node before sending the data. This connectionless nature makes Ethernet simpler and more efficient, but it also means that Ethernet itself does not provide any reliability guarantees.

  3. Media Access Control (MAC) : Ethernet uses CSMA/CD (Carrier Sense Multiple Access/Collision Detection) as its media access control method. When an Ethernet node needs to send data, it first listens to the media (usually the cable) to detect if other nodes are sending data. If not, it starts sending data. If two nodes start sending data at the same time, a conflict will occur. At this time, the node will stop sending and wait for a random time before trying to send again.

  4. Frame structure : The structure design of the Ethernet frame is simple and efficient, including fields such as destination address, source address, type/length, and data load. In addition, there is a frame check sequence field for error detection. This frame structure not only meets the basic needs of data transmission, but also ensures overall simplicity.

  5. Scalability : Ethernet is also designed to be highly scalable. For example, the original Ethernet only supported a transmission rate of 10Mbps, but with the advancement of technology, Ethernet can now support rates of 100Mbps, 1Gbps, or even more than 10Gbps. In addition, Ethernet supports a variety of physical media, including twisted pairs, fiber optics, and radio waves.

The above are part of the design ideas and technical details of the Ethernet protocol. Overall, the design of Ethernet emphasizes simplicity, connectivity, and scalability, allowing it to be widely used in various network environments.

3. IP protocol

3.1 Functions and characteristics of IP protocol

Internet Protocol (IP) is the core protocol of the Internet Protocol suite (TCP/IP) and is responsible for sending data encapsulated in IP datagrams from the source host to the destination host. Its core functions include address identification, fragmentation and reassembly, routing and error handling, etc.

  • Address identification : The IP address is the unique identification of the device in the network, allowing datagrams to be accurately routed from the source host to the destination host. IP addresses are divided into two formats: IPv4 (32 bits) and IPv6 (128 bits).

  • Fragmentation and reassembly : The IP protocol provides fragmentation and reassembly mechanisms, which can decompose large datagrams into smaller datagrams for transmission, and reassemble these datagrams at the receiving end to adapt to different networks. The maximum transmission unit (MTU) limit in your environment.

  • Routing : A key function of the IP protocol is routing, which selects the best path for datagrams from the source address to the destination address based on the information in the routing table.

  • Error handling : The IP protocol has the ability to handle errors that occur during datagram transmission, but it is not responsible for error recovery. If an error occurs during datagram transmission, IP will notify the sender by generating an ICMP message, but the task of retransmitting the erroneous datagram is usually the responsibility of the upper layer protocol (such as TCP).

In addition to the core functions mentioned above, the IP protocol has some additional features and capabilities. For example, the IP protocol is connectionless, which means that the host sending the datagram does not need to establish a connection with the receiving host before sending. In addition, the IP protocol is also a best effort. It does not guarantee the reliability of datagram transmission, does not provide error recovery, and does not perform flow control or congestion control. These tasks are handed over to upper-layer protocols, such as TCP.

3.2 IP protocol header

The IP protocol header contains all important information required for datagram transmission. In IPv4, the length of the protocol header is fixed at 20 bytes (unless the option field is used), while in IPv6, the length of the header is fixed at 40 bytes. The main fields of the protocol header include:

  • Version : Indicates the version of the IP protocol, IPv4 or IPv6.

  • Header length : Indicates the length of the IP header.

  • Service type/traffic level : In IPv4, this field is called the type of service, which is used to indicate the priority and QoS requirements of the datagram; in IPv6, this field is divided into traffic classes (Traffic Class) and flow label (Flow Label) two parts.

  • Total length : indicates the whole

The length of a datagram (including header and data).

  • Time to Live/Hop Limit : In IPv4, this field is called Time to Live (TTL), which is used to limit the survival time of datagrams in the network; in IPv6, this field is called Hop Limit (Hop Limit). Limit).

  • Protocol/Next Header : In IPv4, this field is called protocol and is used to indicate the type of upper layer protocol; in IPv6, this field is called next header.

  • Header checksum : used to check whether there are errors in the header during transmission.

  • Source address and destination address : indicate the IP addresses of the sending host and receiving host of the datagram respectively.

3.3 IP protocol options and extensions

The IP protocol provides some options and extensions to support more functions. In IPv4, these options include but are not limited to source routing options, record routing options, timestamp options, etc. However, since these options may increase the complexity of the protocol and introduce security issues, they are not used much in practice.

In IPv6, most options were moved out of the basic header and placed into the extension header in order to simplify the header and improve processing speed. IPv6 defines a variety of extension headers, such as Hop-by-Hop Options, Routing, Fragment, Authentication, Encapsulating Security Payload, etc.

4. UDP protocol

User Datagram Protocol (UDP) is a simple datagram-oriented transport layer protocol that provides a connectionless, best-effort data transmission service. The following is a detailed introduction to the UDP protocol:

4.1 Functions and characteristics of UDP protocol

The main function of the UDP protocol is to provide a simple and fast data transmission service. Although the UDP protocol does not provide any guarantees of data reliability, sequence, or data integrity, it is precisely because of this simplicity that UDP has advantages in many application scenarios. For example, for some applications that have high real-time requirements and can tolerate a certain amount of packet loss, such as VoIP (voice calls), video conferencing, online games, etc., UDP is preferred.

On the other hand, the UDP protocol is connectionless, which means that there is no process of establishing and disconnecting connections between sending and receiving data, and data can be sent at any time. This feature is suitable for some request/response mode services, such as DNS queries. very useful.

The UDP protocol also supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication modes.

4.2 UDP datagram

UDP datagram consists of two parts: header and data. The length of the header is fixed at 8 bytes. The header contains the following fields:

  • Source port and destination port : indicate the port numbers of the sending and receiving ends of the datagram respectively. Port numbers are used to distinguish different applications on the host.

  • Length : Indicates the length of the entire UDP datagram (including header and data).

  • Checksum : used to check whether there are errors in the datagram during transmission. Although this is an optional field, it is commonly used in practice.

4.3 Relationship between UDP and IP protocols

The UDP protocol works at the transport layer, and it relies on the IP protocol of the network layer to complete the sending and receiving of datagrams. The UDP protocol encapsulates application layer data into datagrams. After adding the UDP header, the datagram is handed over to the IP protocol. The IP protocol sends the datagram to the target host based on the destination IP address and routing table of the UDP datagram. After the target host receives the datagram, the IP protocol hands the datagram to the UDP protocol, and the UDP protocol removes the header and hands the data to the application layer.

5. Summary of UDP protocol

In the Internet, data transmission involves multiple network levels. Each layer has its own responsibilities and characteristics, working together to support the sending and receiving of data. In this process, MAC address, IP address and port number all play an important role.

5.1 MAC address

The MAC address is used in the data link layer (Ethernet layer) and is a unique identifier for a network interface controller (also called a network interface card, NIC). In Ethernet, data is sent and received in the form of frames. The header of the frame contains the source MAC address and the destination MAC address. This ensures that the frame can be sent correctly from the source host to the destination host.

5.2 IP address

The IP address is used in the network layer and is the unique identifier of the host in the network. When a datagram needs to be sent from the source host to the destination host, the routing path is determined based on the IP address of the destination host. The header of the IP datagram contains the source IP address and the destination IP address, so as to ensure that the datagram can be sent to the target host according to the correct routing path.

5.3 Port number

Port numbers are used in the transport layer (TCP or UDP) to identify a specific application on the host. Since there may be multiple applications on a host using network services at the same time, port numbers are needed to distinguish these applications. In the header of the TCP or UDP datagram, the source port number and the destination port number are included to ensure that the data can be sent to the correct application.

5.4 Summary of UDP protocol

Reviewing the above content, we can see that the UDP protocol serves as a transport layer protocol, and its responsibility is to provide data transmission services between applications. The UDP protocol is connectionless and provides a best-effort service, that is, it does not guarantee the reliability, ordering, or integrity of data. However, it is precisely because of this simplicity that the UDP protocol has advantages in many scenarios, such as VoIP, video conferencing, online games, etc. On the other hand, the UDP protocol also relies on the IP protocol at the network layer and the Ethernet protocol at the data link layer to complete its work. In general, the UDP protocol is an important part of the Internet protocol stack. Although its function is relatively simple, it plays an important role in network communication.

6. UDP case

The following is an example of a simple UDP communication program written in C language, with client and server side codes respectively. We will use the socket API, which is a general network programming interface, when sending and receiving datagrams.

6.1 UDP server

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 8888  // 服务器端口号
#define MAXLINE 1024  // 缓冲区大小

int main() {
    
    
    int sockfd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len;
    char buffer[MAXLINE];
    ssize_t n;

    // 创建socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
    
    
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&server_addr, 0, sizeof(server_addr));
    memset(&client_addr, 0, sizeof(client_addr));

    // 服务器地址和端口的设置
    server_addr.sin_family = AF_INET;  // IPv4
    server_addr.sin_addr.s_addr = INADDR_ANY;  // 本机的任意IP
    server_addr.sin_port = htons(PORT);  // 端口号

    // 绑定socket和地址
    if (bind(sockfd, (const struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
    
    
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    while (1) {
    
    
        printf("Waiting for data...\n");

        client_addr_len = sizeof(client_addr);

        // 接收客户端的数据报
        n = recvfrom(sockfd, (char*)buffer, MAXLINE, 0, (struct sockaddr*)&client_addr, &client_addr_len);
        buffer[n] = '\0';  // 添加字符串结束符

        printf("Client: %s\n", buffer);

        // 将数据报发送回客户端
        sendto(sockfd, (char*)buffer, strlen(buffer), 0, (struct sockaddr*)&client_addr, client_addr_len);

        if (strncmp("exit", buffer, 4) == 0) {
    
    
            printf("Server Exit...\n");
            break;
        }
    }

    close(sockfd);

    return 0;
}

6.2 UDP client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 8888  // 服务器端口号
#define MAXLINE 1024  // 缓冲区大小

int main() {
    
    
    int sockfd;
    struct sockaddr_in server_addr;
    char buffer[MAXLINE];
    ssize_t n;

    // 创建socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
    
    
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&server_addr, 0, sizeof(server_addr));

    // 服务器地址和端口的设置
    server_addr.sin_family = AF_INET;  // IPv4
    server_addr.sin_addr.s_addr = INADDR_ANY;  // 服务器的IP地址
    server_addr.sin_port = htons(PORT);  // 端口号

    while (1) {
    
    
       

 printf("Enter message: ");
        fgets(buffer, sizeof(buffer), stdin);

        // 将数据报发送到服务器
        sendto(sockfd, (char*)buffer, strlen(buffer), 0, (struct sockaddr*)&server_addr, sizeof(server_addr));

        // 接收服务器的数据报
        n = recvfrom(sockfd, (char*)buffer, MAXLINE, 0, NULL, NULL);
        buffer[n] = '\0';  // 添加字符串结束符

        printf("Server: %s\n", buffer);

        if (strncmp("exit", buffer, 4) == 0) {
    
    
            printf("Client Exit...\n");
            break;
        }
    }

    close(sockfd);

    return 0;
}

Note: In actual operation, the server and client should run on different machines or different processes. You need to replace the server IP address in the client code with the actual server IP address.

In this case, both the UDP server and client use infinite loops to continuously receive and send datagrams. When "exit" is entered, the loop will be terminated and the server and client will exit.

In the above example, we used the following important socket API functions:

  • socket(): Create a new socket.
  • bind(): Bind the socket to a specific IP address and port number.
  • sendto(): Send datagrams to a specific address.
  • recvfrom(): Receive datagrams from a specific address.

This simple example shows how to use the UDP protocol to send and receive data, but please note that the UDP protocol does not provide any guarantees of the reliability, ordering, or integrity of the data. In actual applications, if these features are needed, it may be necessary to use the TCP protocol or implement these features at the application layer.

Guess you like

Origin blog.csdn.net/weixin_52665939/article/details/131506118