TCP (Transmission Control Protocol) connection information management method and device based on Netlink
Through the Netlink-based TCP connection information management method, the window size and maximum segmentation parameters of the TCP transmission protocol are dynamically adjusted, which solves the problem of the TCP protocol being too conservative in a large bandwidth environment, and improves data transmission efficiency.
Patent Information
- Application Number
- CN202411752911.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-02
- Publication Date
- 2025-03-18
AI Technical Summary
When facing a large bandwidth environment, the TCP transmission protocol is too conservative to adjust the size of the transmission window, which hinders the transmission of data volume and reduces the data transmission efficiency of the TCP protocol.
Through the TCP connection information management method based on Netlink, data packets of the TCP transmission protocol are obtained, serial number, confirmation number and initial sending window are analyzed, and the sending window and maximum segmentation parameters are dynamically adjusted to adapt to changes in the network environment.
It improves the data transmission efficiency of TCP protocol in large bandwidth environments, and avoids data transmission obstacles caused by excessive conservative window size adjustment.
Smart Images

Figure CN119652829A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of TCP connection information management, and in particular to a TCP connection information management method and device based on Netlink. Background Art
[0002] At present, with the rapid development of the Internet, TCP (Transmission Control Protocol), as a connection-oriented, reliable transport layer protocol, has become an important cornerstone of data communication. TCP, transmission control protocol, is a connection-oriented, reliable, byte stream-based transport layer communication protocol. It is widely used on the Internet. It is a protocol for reliable data transmission between different hosts. The development of the transmission control protocol has gone through the simple version of TCP (RFC761), TCP optimized version (RFC 762), TCP upgraded version (RFC 793), TCP Reno (RFC 1337), TCP NewReno (RFC6528), and the currently widely used TCP CUBIC (RFC 6298). TCP CUBIC is the mainstream transmission control protocol of the current transport layer.
[0003] In one prior art, the window size adjustment algorithm of the TCP protocol is jointly determined by the sender and the receiver. The sender adjusts the sender window according to the receive window size in the receiver's TCP header; the receiver adjusts the receive window size according to the amount of data sent by the sender.
[0004] In the prior art, because the TCP transmission protocol is too conservative when adjusting the sending window size in a large bandwidth environment, the sending of data volume is hindered, resulting in a reduction in the data transmission efficiency of the TCP protocol. Summary of the invention
[0005] The present invention provides a TCP connection information management method and device based on Netlink, which solves the problem that the transmission of data volume is hindered due to the TCP transmission protocol being too conservative when adjusting the transmission window size in the face of a large bandwidth environment, resulting in reduced data transmission efficiency of the TCP protocol.
[0006] In a first aspect, in order to solve the above technical problems, the present invention provides a TCP connection information management method based on Netlink, comprising:
[0007] Get the application process;
[0008] According to the application process, establish a connection with the system to obtain TCP connection information;
[0009] Get the initial Netlink socket;
[0010] According to the initial Netlink socket, a blocking mode is set to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and a message data content data structure;
[0011] Sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket to obtain a TCP data packet including a TCP state;
[0012] Analyze the TCP data packets one by one to obtain the sequence number, confirmation number and initial sending window;
[0013] Input the sequence number and the confirmation number into the application process for setting, and obtain TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjustment sending window and the current maximum segment;
[0014] Sending the sequence number and the confirmation number to a specified destination address and port, and obtaining return information from the destination address and port;
[0015] A connection is established with the destination address and port according to the returned information and the TCP transmission parameters to achieve data transmission.
[0016] In an optional implementation, the inputting the sequence number and the confirmation number into the application process for setting to obtain TCP transmission parameters including a data frame sequence number, a confirmation frame sequence number, an adjusted sending window, and a current maximum segmentation includes:
[0017] Input the sequence number into a preset sequence number calculation formula for calculation to obtain a data frame sequence number and a confirmation frame sequence number;
[0018] Get the current network status;
[0019] Judging according to the network status, obtaining a normal network environment and an abnormal network environment;
[0020] When the network state is the normal network environment, the initial sending window is set as the adjusted sending window to obtain the adjusted sending window, the system default segmentation is set as the maximum segmentation to obtain the current maximum segmentation;
[0021] When the network state is the abnormal network environment, the sequence number and the confirmation number are input into a preset window calculation formula for calculation to obtain an adjusted sending window;
[0022] The adjusted sending window is input into the segmentation calculation formula for calculation to obtain the current maximum segmentation.
[0023] In an optional implementation, the step of inputting the sequence number into a preset sequence number calculation formula for calculation to obtain the confirmation frame sequence number includes:
[0024] The confirmation frame number is calculated as follows:
[0025] r = n * Seq
[0026] Among them, r is the confirmation frame sequence number, n is the number of data in the message data content data structure, and Seq is the sequence number.
[0027] In an optional implementation, the step of inputting the sequence number into a preset sequence number calculation formula for calculation to obtain the data frame sequence number includes:
[0028] The data frame sequence number is calculated in the following way:
[0029] s=r+1
[0030] Among them, s is the data frame sequence number and r is the confirmation frame sequence number.
[0031] In an optional implementation, inputting the sequence number and the confirmation number into a preset window calculation formula for calculation according to the abnormal network environment to obtain an adjusted sending window includes:
[0032] The adjusted send window is calculated as follows:
[0033] Snew=Seq-Ack
[0034] Among them, Snew is the adjusted sending window, Seq is the sequence number, and Ack is the confirmation number.
[0035] In an optional implementation manner, the inputting the adjusted sending window into a segmentation calculation formula for calculation to obtain a current maximum segmentation includes:
[0036] The current maximum segment is calculated as follows:
[0037] Segment = Snew-SendSeq
[0038] Among them, Segment is the current maximum segment, Snew is the adjusted sending window, and SendSeq is the process sending sequence.
[0039] In an optional implementation manner, the determining according to the network status to obtain a normal network environment and an abnormal network environment includes:
[0040] Get timer data;
[0041] Counting according to the timer data to obtain the number of TCP data packets sent per second;
[0042] The number of TCP data packets sent per second is input into the packet loss calculation formula to calculate and obtain the packet loss rate;
[0043] Input the confirmation number into the ACK rate calculation formula to calculate and obtain the ACK rate;
[0044] The packet loss rate and the ACK rate are compared with a threshold value. When the packet loss rate or the ACK rate does not exceed the threshold value, the network environment is determined to be normal, and a normal network environment is obtained; when the packet loss rate or the ACK rate exceeds the threshold value, the network environment is determined to be abnormal, and an abnormal network environment is obtained.
[0045] In an optional implementation manner, the number of TCP data packets sent per second is input into a packet loss calculation formula for calculation to obtain a packet loss rate, including:
[0046] The packet loss rate is calculated as follows:
[0047] Lost=|sr-Ack| / T
[0048] Among them, Lost is the packet loss rate, r is the confirmation frame number, s is the data frame number, Ack is the confirmation number, and T is the number of data packets sent per second.
[0049] In an optional implementation manner, the step of inputting the confirmation number into an ACK rate calculation formula for calculation to obtain the ACK rate includes:
[0050] The ACK rate is calculated by:
[0051] ACKNum=Ack-SendSeq
[0052] Among them, ACKNum is the ACK rate, SendSeq is the process sending number sequence, and Ack is the confirmation number in the data packet.
[0053] In a second aspect, the present invention provides a TCP connection information management device based on Netlink, comprising:
[0054] Process acquisition module, used to obtain application processes;
[0055] A system connection module, used to establish a connection with the system according to the application process and obtain TCP connection information;
[0056] Socket acquisition module, used to obtain the initial Netlink socket;
[0057] A mode setting module, used for setting a blocking mode according to the initial Netlink socket to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and message content data;
[0058] A data sending module, used for sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket, to obtain a TCP data packet containing a TCP state;
[0059] A data acquisition module, used to analyze the TCP data packets one by one to obtain the sequence number, confirmation number and initial sending window;
[0060] A parameter acquisition module, used for inputting the sequence number and the confirmation number into the application process for setting, and obtaining TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjustment sending window and the current maximum segment;
[0061] A data receiving module, used for sending the sequence number and the confirmation number to a specified destination address and port, and obtaining return information from the destination address and port;
[0062] The connection transmission module is used to connect to the destination address and port according to the return information and the TCP transmission parameters to achieve data transmission.
[0063] In a third aspect, the present invention further provides an electronic device, comprising a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, the Netlink-based TCP connection information management method described in any one of the above is implemented.
[0064] In a fourth aspect, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any one of the above-mentioned Netlink-based TCP connection information management methods.
[0065] Compared with the prior art, the present invention has the following beneficial effects:
[0066] The invention discloses a TCP connection information management method based on Netlink, comprising the steps of obtaining an application process; establishing a connection with a system according to the application process to obtain TCP connection information; obtaining an initial Netlink socket; setting a blocking mode according to the initial Netlink socket to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket comprises a message type field and a message data content data structure; sending TCP information to a system according to the TCP connection information and the blocking mode Netlink socket to obtain a TCP data packet containing a TCP state; analyzing the TCP data packets one by one to obtain a sequence number, a confirmation number and an initial sending window; inputting the sequence number and the confirmation number into an application process for setting to obtain TCP transmission parameters containing a data frame sequence number, a confirmation frame sequence number, an adjusted sending window and a current maximum segmentation; sending the sequence number and the confirmation number to a specified destination address and port to obtain return information from the destination address and port; and connecting with the destination address and port according to the return information and the TCP transmission parameters to realize data transmission. The present invention obtains data of the TCP transmission protocol transmission layer, including TCP flags, sequence numbers, confirmation numbers, and window sizes, and dynamically adjusts the parameters of the TCP transmission protocol, including the sending window and the maximum segmentation, through formula transformation. Compared with the prior art, the TCP protocol window size adjustment algorithm requires the sender and the receiver to jointly decide, the sender adjusts the sender window according to the receiving window size in the receiver's TCP header; the receiver adjusts the receiving window size according to the amount of data sent by the sender. The present invention can solve the problem that the TCP transmission protocol is too conservative when adjusting the sending window size in the face of a large bandwidth environment, the sending of the data volume is hindered, resulting in a reduction in the data transmission efficiency of the TCP protocol. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] Figure 1 It is a schematic flow chart of a method for managing TCP connection information based on Netlink provided by a first embodiment of the present invention;
[0068] Figure 2 It is a schematic diagram of a working scenario of a Netlink-based TCP connection information management method provided by the first embodiment of the present invention;
[0069] Figure 3 It is a schematic diagram of the structure of a Netlink-based TCP connection information management device provided by the second embodiment of the present invention. DETAILED DESCRIPTION
[0070] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0071] Reference Figure 1 The first embodiment of the present invention provides a TCP connection information management method based on Netlink, comprising the following steps:
[0072] S11, obtaining the application process;
[0073] S12, establishing a connection with the system according to the application process, and obtaining TCP connection information;
[0074] S13, obtain the initial Netlink socket;
[0075] S14, according to the initial Netlink socket, setting the blocking mode to obtain the blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and a message data content data structure;
[0076] S15, sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket to obtain a TCP data packet including a TCP state;
[0077] S16, analyzing the TCP data packets one by one to obtain a sequence number, an acknowledgment number, and an initial sending window;
[0078] S17, inputting the sequence number and the confirmation number into the application process for setting, and obtaining TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjusted sending window and the current maximum segment;
[0079] S18, sending the sequence number and the confirmation number to the specified destination address and port, and obtaining return information from the destination address and port;
[0080] S19, connecting to the destination address and port according to the returned information and the TCP transmission parameters to achieve data transmission.
[0081] In step S11, the application process is obtained.
[0082] In the process of obtaining application processes, the specific application program whose information needs to be obtained is identified by the application name, package name or process ID (PID), and then the list of all currently running processes is obtained by using the API provided by the operating system. The process information of the target application is filtered out from the obtained process list. Common fields include: process ID (PID), process name, process status (such as running, suspended, etc.). The obtained process information is sorted and formatted for subsequent use in connecting to the system.
[0083] In step S12, a connection is established with the system according to the application process to obtain TCP connection information.
[0084] TCP (Transmission Control Protocol) is a core protocol in the Internet protocol suite that provides reliable, ordered data transmission and ensures data integrity. In a running application, a TCP connection is usually presented as a series of "endpoints", each of which is identified by an IP address and a port number, through which the application communicates with the system. A TCP connection can be in a variety of states, including but not limited to the state in which the system is waiting for a connection request, the connection between the application process and the system has been successfully established, the state in which data can be transmitted between the two endpoints, and the state in which the connection is closing or has been closed. There are many ways to obtain TCP connection information on the system, which are limited by the operating system. There are mainly the following: 1. Use command line tools; on most Unix / Linux systems, you can use the netstat or ss command to list all TCP connections; 2. Use system calls: In Linux systems, you can directly read TCP connection information through the / proc / net / tcp file, which contains detailed information about the currently active TCP connections; 3. Use third-party libraries: There are many libraries available in Python, such as psutil, which provide a more user-friendly interface to obtain system connection information. After obtaining the TCP connection information, you need to parse, process and store this information. The acquired TCP connection information needs to be formatted into a structure that is easy to read and analyze, and stored in a file or database. After acquiring a large amount of TCP connection data, analyze the data and extract valuable information. In addition, use a scheduled task (such as a cron job) to regularly check the TCP connection information, capture the data and store it. If more real-time feedback is required, a polling mechanism can be used to continuously monitor changes in the TCP connection status in an independent thread or process and update the status.
[0085] In step S13, an initial Netlink socket is obtained.
[0086] Netlink is a mechanism used in the Linux system kernel for communication between user space and kernel space. It provides an efficient and flexible interface for network-related operations, allowing user space programs to interact directly with the kernel. Netlink sockets are a special type of network sockets, mainly used for inter-process communication (IPC) and data exchange between the kernel and user space in Linux systems. Unlike traditional sockets, Netlink provides a more flexible way to access kernel data structures, especially in network management and configuration. Netlink can be used for a variety of operations, such as routing table management, network interface configuration and status monitoring, etc. Netlink supports asynchronous event notification mechanisms, allowing the kernel to push events to user space. The Netlink protocol supports a variety of sub-protocols, such as RTNETLINK, GENL, etc., which are suitable for different network management needs. In user space, you first need to create a Netlink socket, which is usually done by calling the socket() function. After creating the socket, you need to bind it to a specific address to facilitate subsequent communication. You can use the bind() function to bind the socket to the address. Once the Netlink socket is successfully created and bound, you can start sending and receiving messages.
[0087] In step S14, a blocking mode is set according to the initial Netlink socket to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and a message data content data structure.
[0088] In network programming, Netlink sockets are a powerful tool for communication between user space and kernel space. By setting the Netlink socket to blocking mode, you can effectively manage and process messages from the kernel and avoid constant polling to check for available data. Blocking mode means that when there is no data available to read, calling a receiving function (such as recv() or recvmsg()) will cause the calling thread to be suspended until data is available for processing. This mode is often used in application scenarios where data integrity and sequence need to be guaranteed, such as network monitoring, routing table updates, etc. Before you start, you need to make sure that an initial Netlink socket has been created. In Linux systems, sockets are opened in blocking mode by default, but if you need to explicitly control this feature, you can use the fcntl() function to set the socket's properties. In order to effectively transmit data, you need to define a suitable message structure, including the message type field and the message data content. Here, the Netlink message header and payload can be used to represent this information. In Netlink, the message type field is usually specified by the nlmsg_type field in the message header. Depending on different application scenarios, multiple message types can be defined. For example: Request type: indicates a message from user space that sends a request; Response type: indicates a message returned by the kernel to user space. After defining the message type, you need to define the data content structure of the message. Usually, this structure will contain the actual data that needs to be transmitted. Through the above steps, a blocking mode Netlink socket can be successfully created and a message structure suitable for transmission can be defined. In practical applications, this communication mechanism is very flexible and can be used in a variety of scenarios, such as network status monitoring, routing control, network configuration, etc. When performing network programming, blocking mode can often simplify the logic of data reception, so that the program does not waste CPU resources when waiting for data. In addition, the design of Netlink allows efficient and reliable exchange of information between user space and kernel space.
[0089] In step S15, TCP information is sent to the system according to the TCP connection information and the blocking mode Netlink socket to obtain a TCP data packet including a TCP state.
[0090] In modern network systems, Linux provides a rich set of kernel APIs to monitor and manage TCP connection status. Netlink sockets are an important mechanism for exchanging information with the kernel. Netlink sockets work based on messages. They use a hierarchical structure to organize data messages. Each message contains a header and a payload. In order to obtain the TCP status, you first need to define the TCP connection information, which usually includes: source IP address, source port, destination IP address, destination port. This information can be obtained through some system calls such as getaddrinfo() or directly from the socket of the application layer. In the Netlink socket, each message sent has a certain format. Good messages need to be constructed as required and filled with TCP connection information. A piece of memory is allocated for Netlink messages, and the message header and TCP specific information are correctly filled. After the message is built, the message needs to be sent to the Netlink socket. Since we set the blocking mode, we will wait until the sending is successful. After sending the request, we are ready to receive the TCP status information returned by the kernel. Because the blocking mode is used, we will wait until there is data to read. The received data contains TCP status information, which can be parsed according to specific needs. In the received Netlink message, the TCP status information needs to be parsed, and the TCP status is analyzed according to the specified format. The status information is parsed in the following way: first, check the message header to confirm whether the received message is valid and whether its type is the TCP information we expect; after obtaining valid TCP information, the status field can be extracted from it. The TCP status is usually represented by an integer value corresponding to different states; after completing the processing of TCP information, clean up the allocated memory to avoid memory leaks. This mechanism is used in many network management and monitoring tools. For example, network administrators may need to monitor the status of TCP connections in real time to quickly detect and solve connection problems. In this way, more complex network diagnostic tools can also be integrated to help users understand the health of the network.
[0091] In step S16, the TCP data packets are analyzed one by one to obtain the sequence number, confirmation number and initial sending window.
[0092] TCP (Transmission Control Protocol) is a connection-oriented protocol that guarantees data reliability and order. After effectively parsing this information, the network status can be better understood and diagnosed. TCP packets can be analyzed by capturing the data stream on the network. This can be achieved through a variety of tools, such as tcpdump or Wireshark, or by using libraries such as libpcap to capture in a C program. After we successfully capture a TCP packet, we can analyze it in the following ways. First, we need to define the structure of the TCP header to facilitate the extraction of relevant information from the captured packet; extract the information of the TCP header from the captured packet. When determining the length of the IP header, we can directly locate the TCP header. The sequence number is a key field in the TCP packet, which is used to track the byte order in the data stream. TCP is a byte stream protocol, so each packet has a corresponding sequence number to ensure that the receiver can reassemble the data in the correct order. The sequence number enables the receiver to determine which data has been successfully received and which needs to be re-requested. For example: when a TCP session starts, the two parties will exchange the initial sequence number through the three-way handshake (SYN) process. After the connection is established, all packets carry sequence numbers, and the receiver determines whether there is data loss based on these sequence numbers. The confirmation number indicates the sequence number of the next data byte that the receiver expects to receive, which can prevent data loss and retransmission. When the receiver successfully receives a data packet, it will send a confirmation number to the sender to inform it that the data has been received and expect the subsequent data bytes. For example: during data transmission, if the sender sends a data packet with a sequence number of 100, the receiver will return an ACK packet with a confirmation number of 101 after receiving it, indicating that it has successfully received byte 100. Initial Send Window The window size determines how many bytes of data the receiver can receive at the same time. It is an important mechanism for implementing flow control and avoiding network congestion. By dynamically adjusting the window size, TCP can control the data transmission rate to ensure that the sender does not exceed the processing capacity of the receiver. For example: assuming that the window size set by the receiver is 5000 bytes, if the sender attempts to send more than this amount of data, it must wait for the receiver's ACK to ensure that no data loss or overflow occurs.
[0093] In step S17, the sequence number and the confirmation number are input into the application process for setting, and TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjusted sending window and the current maximum segment are obtained.
[0094] In one implementation, the sequence number is input into a preset sequence number calculation formula for calculation to obtain a data frame sequence number and a confirmation frame sequence number; the current network status is obtained; a judgment is made based on the network status to obtain a normal network environment and an abnormal network environment; when the network status is the normal network environment, the initial sending window is set to an adjusted sending window to obtain an adjusted sending window, and the system default segmentation is set to a maximum segmentation to obtain a current maximum segmentation; when the network status is the abnormal network environment, the sequence number and the confirmation number are input into a preset window calculation formula for calculation to obtain an adjusted sending window; the adjusted sending window is input into a segmentation calculation formula for calculation to obtain the current maximum segmentation.
[0095] It should be noted that the judgment based on the network status to obtain a normal network environment and an abnormal network environment includes: obtaining timer data; counting according to the timer data to obtain the number of TCP data packets sent per second; inputting the number of TCP data packets sent per second into a packet loss calculation formula for calculation to obtain a packet loss rate; inputting the confirmation number into an ACK rate calculation formula for calculation to obtain an ACK rate; comparing the packet loss rate and the ACK rate with a threshold, when the packet loss rate or the ACK rate does not exceed the threshold, determining that the network environment is normal and obtaining a normal network environment; when exceeding the threshold, determining that the network environment is abnormal and obtaining an abnormal network environment.
[0096] It should be noted that the confirmation frame number is calculated in the following way:
[0097] r=n*Seq
[0098] Wherein, r is the confirmation frame sequence number, n is the message content data, and Seq is the sequence number.
[0099] The data frame sequence number is calculated in the following way:
[0100] s=r+1
[0101] Among them, s is the data frame sequence number and r is the confirmation frame sequence number.
[0102] The adjusted send window is calculated as follows:
[0103] Snew=Seq-Ack
[0104] Among them, Snew is the adjusted sending window, Seq is the sequence number, and Ack is the confirmation number.
[0105] The current maximum segment is calculated as follows:
[0106] Segment = Snew-SendSeq
[0107] Among them, Segment is the current maximum segment, Snew is the adjusted sending window, and SendSeq is the process sending sequence.
[0108] The packet loss rate is calculated as follows:
[0109] Lost=|sr-Ack| / T
[0110] Among them, Lost is the packet loss rate, r is the confirmation frame number, s is the data frame number, Ack is the confirmation number, and T is the number of data packets sent per second.
[0111] The ACK rate is calculated by:
[0112] ACKNum=Ack-SendSeq
[0113] Among them, ACKNum is the ACK rate, SendSeq is the process sending number sequence, and Ack is the confirmation number in the data packet.
[0114] In modern network communications, the TCP protocol manages data flows through mechanisms such as sequence numbers, acknowledgment numbers, and window control. The sequence number is entered through a preset formula to calculate the data frame sequence number and the acknowledgment frame sequence number. Before any further operations are performed, the current network status must be evaluated. In this case, the network monitoring tool or TCP library function is used to obtain timer data, collect the number of data packets sent within a certain period of time, calculate the number sent per second, and calculate the packet loss rate and ACK rate through a preset formula. By analyzing the above packet loss rate and ACK rate, it is determined whether the network environment is normal or abnormal. By writing scripts or using network management software, a threshold is set. Once the packet loss rate exceeds the preset value, the sending window size is automatically reduced, and the number of retransmissions is increased, which can reduce network congestion. Once the network status is determined, in a normal network environment, the initial sending window is set to the adjusted sending window. At this time, a larger window size is maintained, and the system default segmentation is the current maximum segmentation to improve data transmission efficiency. In an abnormal network environment, the sequence number and acknowledgment number are entered into the preset window calculation formula, and the adjusted sending window is calculated again. The current maximum segmentation is continued to be calculated based on the adjusted sending window. Under normal network conditions, the window size and segmentation can be maintained at a high level to improve throughput; under abnormal network conditions, by adjusting these parameters, excessive data loss can be avoided and data integrity and transmission reliability can be ensured.
[0115] Among them, the confirmation frame sequence number is the frame sequence number used to confirm that the receiver has successfully received the data. In the TCP protocol, the confirmation sequence number is an important feedback mechanism that indicates the control and integrity of the data flow. The message content data indicates the amount of data sent this time, usually in bytes, which can reflect the length of the data sent. The sequence number is a unique identifier assigned by the sender to each data segment to ensure the order and integrity of the data. Each time a data segment is sent, the sequence number will be added to the number of bytes of the data segment accordingly. The data frame sequence number is the sequence number of the data frame currently sent, which is usually incremented immediately after the confirmation frame to maintain the consistency of the data packet sequence. The confirmation frame sequence number can be calculated by a formula to ensure the transmission order of the data. Adjusting the send window indicates the amount of data that the host is allowed to continue to send without receiving confirmation. Dynamically adjusting the send window size can effectively utilize the network bandwidth. The confirmation number indicates the next sequence number of the data that the receiver has successfully received, which helps the sender understand which data has been confirmed to be received, so as to determine the further amount of data to send. The current maximum segment refers to the maximum amount of data that can be sent at one time under the current network environment, usually expressed in bytes. Optimizing this value helps improve the throughput of the network. The process send sequence number represents the data sequence number that has been sent but not yet confirmed, which helps calculate the available sending space. The packet loss rate reflects the proportion of data packets lost during the transmission process and is an important indicator for judging network quality. A high packet loss rate can lead to problems such as TCP retransmission. The number of data packets sent per second represents the number of data packets successfully sent in the network within a given time, which is used as a benchmark to calculate the loss ratio. The ACK rate represents the number of confirmed data packets received and is an important indicator for measuring the feedback capability of the network.
[0116] In step S18, the sequence number and the confirmation number are sent to the specified destination address and port, and return information from the destination address and port is obtained.
[0117] Before sending the sequence number and confirmation number, a TCP segment needs to be constructed, which contains the necessary header information, such as source port, destination port, sequence number, confirmation number, etc. The two fields of source port and destination port are used to identify the two ends of the communication. The source port is usually dynamically assigned by the operating system, while the destination port is the port used by the receiving application (for example, HTTP usually uses port 80 and HTTPS uses port 443). Fill the current sequence number and confirmation number into the corresponding fields of the TCP segment, and set the flag bits in the TCP segment according to the current communication status. Determine which network interface to send the data through. If the device has multiple network interfaces (such as Wi-Fi, Ethernet), select the appropriate interface to ensure that the data can be transmitted correctly. Encapsulate the constructed TCP segment in an IP data packet and send it through the underlying network protocol. At this time, the operating system calls the network stack to send the data to the network. After sending, the sender will enter a waiting state, expecting to receive return information from the specified destination address and port. During this process, if the return information is not received within a certain period of time, it may be considered that packet loss or network failure has occurred, and measures such as retransmission need to be taken. After receiving the return information from the destination address and port, it needs to be processed. Extract the sequence number, confirmation number and other important information from the returned message. According to the TCP protocol specification, the returned information may include a confirmation number, indicating that the receiver has successfully received the data. Update the local sending status based on the received confirmation number. For example, if the confirmation number is greater than the current sequence number, it means that the previously sent data has been successfully received.
[0118] In step S19, a connection is made with the destination address and port according to the return information and the TCP transmission parameters to achieve data transmission.
[0119] Clearly define the target address and port of the connection. After obtaining this information and collecting the necessary TCP transmission parameters, establish a connection with the target address and port by calling the connect() method. During this process, TCP will automatically perform a three-way handshake to ensure the reliability of the connection. Among them, the three-way handshake includes: the client sends a SYN packet to the server, indicating a request to establish a connection; the server responds with a SYN-ACK packet to confirm that the request has been received and also requests to establish a connection; the client sends an ACK packet to confirm that the connection is established. After the connection is completed, the client and server can start two-way data transmission. When the data transmission is completed, both parties will disconnect through a series of packets to release system resources.
[0120] In order to facilitate the understanding of the present invention, some preferred embodiments of the present invention are further described below.
[0121] The following describes the working process of the present invention using a common scenario as an example. Figure 2, which is Figure 1 Schematic diagram of the working scenario of the method.
[0122] Develop a chat application based on Netlink TCP connection information management, supporting multiple users to communicate online at the same time. The application will have a server and multiple clients. The server is responsible for managing all connections and forwarding messages to each client. Listen to a specific port and accept connection requests from the client. Whenever a client connects, the server creates a new thread to process the client's messages and maintains a list of active users. The user enters a message through the client interface. After the client establishes a connection with the server, it can send and receive messages from other users. The user can connect to the chat server. When the user logs out, the server can detect and clear the user's connection. The user enters a message and sends it to the server. After the server receives the message, it broadcasts it to all other connected users. It can support multiple user connections at the same time. The server uses multi-threading to process the messages of each client to ensure that there is no delay in the data transmission process. The administrator starts the chat server and specifies the IP address and port to listen to. The user runs the client program and enters the server's IP address and port number to connect. After the client successfully connects, a welcome message is displayed on the screen. The user enters the message in the text box and presses the send button, and the message is sent to the server. The server broadcasts the message to all other connected clients, and all users can see the new message in real time. The user can choose to exit the chat, the client sends a disconnect request to the server, and the server closes the user's connection.
[0123] In summary, the present invention discloses a TCP connection information management method based on Netlink, including obtaining an application process; establishing a connection with a system according to the application process to obtain TCP connection information; obtaining an initial Netlink socket; setting a blocking mode according to the initial Netlink socket to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and a message data content data structure; sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket to obtain a TCP data packet containing a TCP state; analyzing the TCP data packets one by one to obtain a sequence number, a confirmation number and an initial sending window; inputting the sequence number and the confirmation number into the application process for setting to obtain TCP transmission parameters including a data frame sequence number, a confirmation frame sequence number, an adjusted sending window and a current maximum segment; sending the sequence number and the confirmation number to a specified destination address and port to obtain return information from the destination address and port; connecting to the destination address and port according to the return information and the TCP transmission parameters to realize data transmission. The present invention obtains data of the TCP transmission protocol transmission layer, including TCP flags, sequence numbers, confirmation numbers, and window sizes, and dynamically adjusts the parameters of the TCP transmission protocol, including the sending window and the maximum segmentation, through formula transformation. Compared with the prior art, the TCP protocol window size adjustment algorithm requires the sender and the receiver to jointly decide, the sender adjusts the sender window according to the receiving window size in the receiver's TCP header; the receiver adjusts the receiving window size according to the amount of data sent by the sender. The present invention can solve the problem that the TCP transmission protocol is too conservative when adjusting the sending window size in the face of a large bandwidth environment, the sending of the data volume is hindered, resulting in a reduction in the data transmission efficiency of the TCP protocol.
[0124] Reference Figure 3 The second embodiment of the present invention provides a TCP connection information management device based on Netlink, comprising:
[0125] Process acquisition module, used to obtain application processes;
[0126] A system connection module, used to establish a connection with the system according to the application process and obtain TCP connection information;
[0127] Socket acquisition module, used to obtain the initial Netlink socket;
[0128] A mode setting module, used for setting a blocking mode according to the initial Netlink socket to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field msgt and message content data;
[0129] A data sending module, used for sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket, to obtain a TCP data packet containing a TCP state;
[0130] A data acquisition module, used to analyze the TCP data packets one by one to obtain the sequence number, confirmation number and initial sending window;
[0131] A parameter acquisition module, used for inputting the sequence number and the confirmation number into the application process for setting, and obtaining TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjustment sending window and the current maximum segment;
[0132] A data receiving module, used for sending the sequence number and the confirmation number to a specified destination address and port, and obtaining return information from the destination address and port;
[0133] The connection transmission module is used to connect to the destination address and port according to the return information and the TCP transmission parameters to achieve data transmission.
[0134] It should be noted that the Netlink-based TCP connection information management device provided in the embodiment of the present invention is used to execute all the process steps of the Netlink-based TCP connection information management method in the above embodiment. The working principles and beneficial effects of the two correspond one to one, and thus will not be described in detail.
[0135] The embodiment of the present invention further provides an electronic device. The electronic device includes: a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps in the above-mentioned embodiments of the TCP connection information management method based on Netlink are implemented, for example Figure 1 Alternatively, when the processor executes the computer program, the functions of each module / unit in the above-mentioned device embodiments are implemented, such as the connection transmission module.
[0136] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer program in the electronic device.
[0137] The electronic device may be a computing device such as a desktop computer, a notebook, a PDA, and a smart tablet. The electronic device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that the above components are merely examples of electronic devices and do not constitute a limitation on the electronic device. The electronic device may include more or fewer components than the above components, or may combine certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0138] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the electronic device, and uses various interfaces and lines to connect various parts of the entire electronic device.
[0139] The memory can be used to store the computer program and / or module, and the processor realizes various functions of the electronic device by running or executing the computer program and / or module stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area can store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0140] Wherein, if the module / unit integrated in the electronic device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0141] It should be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the accompanying drawings of the device embodiments provided by the present invention, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines. A person of ordinary skill in the art may understand and implement it without paying any creative effort.
[0142] The specific embodiments described above further illustrate the purpose, technical solutions and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for managing TCP connection information based on Netlink, characterized in that: Executed by a computer, including: Get the application process; According to the application process, establish a connection with the system to obtain TCP connection information; Get the initial Netlink socket; According to the initial Netlink socket, a blocking mode is set to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and message content data; Sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket to obtain a TCP data packet including a TCP state; Analyze the TCP data packets one by one to obtain the sequence number, confirmation number and initial sending window; Input the sequence number and the confirmation number into the application process for setting, and obtain TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjustment sending window and the current maximum segment; Sending the sequence number and the confirmation number to a specified destination address and port, and obtaining return information from the destination address and port; A connection is established with the destination address and port according to the returned information and the TCP transmission parameters to achieve data transmission.
2. The method for managing TCP connection information based on Netlink according to claim 1, characterized in that: The step of inputting the sequence number and the confirmation number into the application process for setting, and obtaining TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjustment of the sending window, and the current maximum segmentation, includes: Input the sequence number into a preset sequence number calculation formula for calculation to obtain a data frame sequence number and a confirmation frame sequence number; Get the current network status; Judging according to the network status, obtaining a normal network environment and an abnormal network environment; When the network state is the normal network environment, the initial sending window is set as the adjusted sending window to obtain the adjusted sending window, the system default segmentation is set as the maximum segmentation to obtain the current maximum segmentation; When the network state is the abnormal network environment, the sequence number and the confirmation number are input into a preset window calculation formula for calculation to obtain an adjusted sending window; The adjusted sending window is input into the segmentation calculation formula for calculation to obtain the current maximum segmentation.
3. The method for managing TCP connection information based on Netlink according to claim 2, characterized in that: The step of inputting the sequence number into a preset sequence number calculation formula to calculate and obtain a confirmation frame sequence number includes: The confirmation frame number is calculated as follows: r=n*Seq Wherein, r is the confirmation frame sequence number, n is the message content data, and Seq is the sequence number.
4. The method for managing TCP connection information based on Netlink according to claim 3, characterized in that: The step of inputting the sequence number into a preset sequence number calculation formula to calculate and obtain the data frame sequence number includes: The data frame sequence number is calculated in the following way: s=r+1 Among them, s is the data frame sequence number and r is the confirmation frame sequence number.
5. The method for managing TCP connection information based on Netlink according to claim 2, characterized in that: When the network state is the abnormal network environment, inputting the sequence number and the confirmation number into a preset window calculation formula for calculation to obtain an adjusted sending window includes: The adjusted send window is calculated as follows: Snew=Seq-Ack Among them, Snew is the adjusted sending window, Seq is the sequence number, and Ack is the confirmation number.
6. The method for managing TCP connection information based on Netlink according to claim 5, characterized in that: The adjusting the sending window is input into the segmentation calculation formula for calculation to obtain the current maximum segmentation, including: The current maximum segment is calculated as follows: Segment = Snew-SendSeq Among them, Segment is the current maximum segment, Snew is the adjusted sending window, and SendSeq is the process sending sequence.
7. The method for managing TCP connection information based on Netlink according to claim 2, characterized in that: The determining according to the network status to obtain a normal network environment and an abnormal network environment includes: Get timer data; Counting according to the timer data to obtain the number of TCP data packets sent per second; The number of TCP data packets sent per second is input into the packet loss calculation formula to calculate and obtain the packet loss rate; Input the confirmation number into the ACK rate calculation formula to calculate and obtain the ACK rate; The packet loss rate and the ACK rate are compared with a threshold value. When the packet loss rate or the ACK rate does not exceed the threshold value, the network environment is determined to be normal, and a normal network environment is obtained; when the packet loss rate or the ACK rate exceeds the threshold value, the network environment is determined to be abnormal, and an abnormal network environment is obtained.
8. The method for managing TCP connection information based on Netlink according to claim 7, characterized in that: The number of TCP data packets sent per second is input into the packet loss calculation formula for calculation to obtain the packet loss rate, including: The packet loss rate is calculated as follows: Lost=|sr-Ack| / T Among them, Lost is the packet loss rate, r is the confirmation frame number, s is the data frame number, Ack is the confirmation number, and T is the number of data packets sent per second.
9. The method for managing TCP connection information based on Netlink according to claim 7, characterized in that: The step of inputting the confirmation number into an ACK rate calculation formula to calculate and obtain the ACK rate includes: The ACK rate is calculated by: ACKNum=Ack-SendSeq Among them, ACKNum is the ACK rate, SendSeq is the process sending number sequence, and Ack is the confirmation number in the data packet.
10. A TCP connection information management device based on Netlink, characterized in that: include: Process acquisition module, used to obtain application processes; A system connection module, used to establish a connection with the system according to the application process and obtain TCP connection information; Socket acquisition module, used to obtain the initial Netlink socket; A mode setting module, used for setting a blocking mode according to the initial Netlink socket to obtain a blocking mode Netlink socket; wherein the blocking mode Netlink socket includes a message type field and message content data; A data sending module, used for sending TCP information to the system according to the TCP connection information and the blocking mode Netlink socket, to obtain a TCP data packet containing a TCP state; A data acquisition module, used to analyze the TCP data packets one by one to obtain the sequence number, confirmation number and initial sending window; A parameter acquisition module, used for inputting the sequence number and the confirmation number into the application process for setting, and obtaining TCP transmission parameters including the data frame sequence number, the confirmation frame sequence number, the adjustment sending window and the current maximum segment; A data receiving module, used for sending the sequence number and the confirmation number to a specified destination address and port, and obtaining return information from the destination address and port; The connection transmission module is used to connect to the destination address and port according to the return information and the TCP transmission parameters to achieve data transmission.
Citation Information
Cited By
Method for detecting network connection state of embedded device and embedded device
CN119854174A