Network data flow distribution method and system
By establishing a VPN tunnel and configuring a virtual network card on the mobile device, listening to and parsing data traffic packets, and dynamically distributing them to the appropriate network, the problem of mobile IoT devices and the Internet not being able to be used in parallel is solved, achieving automated network switching and improving the user experience.
Patent Information
- Application Number
- CN202510837586.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-23
- Publication Date
- 2025-08-01
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Mobile devices cannot achieve bidirectional parallel use of IoT devices and the Internet. Existing technologies require users to frequently and manually switch network connections, which affects the user experience.
By establishing a VPN tunnel, configuring a virtual network card, listening to and parsing data traffic packets, and dynamically distributing traffic to the cellular network or the Wi-Fi network of IoT devices based on the target IP address, the bidirectional parallel use of IoT devices and the Internet can be achieved.
It enables the bidirectional parallel use of IoT devices and the Internet without requiring users to manually switch networks, thus improving the user experience.
Smart Images

Figure CN120417113A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data communication, and particularly relates to a method and a system for distributing network data traffic. Background Art
[0002] When an existing mobile device conducts data interaction with an IoT device, data transmission is usually carried out by connecting to a Wi-Fi hotspot provided by the IoT device or a Wi-Fi network located in the same local area network. However, when the mobile device has an Internet access requirement, the user needs to manually switch to another network (such as a cellular network) to disconnect the Wi-Fi connection with the IoT device; if data transmission with the IoT device is required again, the user needs to manually reconnect. Therefore, for users, the operations are frequent, which affects the user experience.
[0003] Another solution is through a single network binding method, that is: binding all traffic to a fixed network interface (such as Wi-Fi) to ensure normal access to the IoT device. Although this method can ensure stable communication of devices within the local area network and does not require frequent user operations, once a single network interface is bound, all data traffic packets will be transmitted through this network interface. If the Wi-Fi network is only used for local area network communication, then the mobile device cannot access the Internet at the same time. Therefore, there is a problem that the mobile device cannot achieve two-way parallel use of the IoT device and the Internet. Summary of the Invention
[0004] In view of the deficiencies in the prior art, the present invention provides a method and a system for distributing network data traffic, which solve the problem that the mobile device cannot achieve two-way parallel use of the IoT device and the Internet.
[0005] To solve the above technical problems, the present invention is solved by the following technical solutions:
[0006] A method for distributing network data traffic includes the following steps:
[0007] Establish a VPN tunnel and configure a virtual network card for intercepting and forwarding event data traffic packets;
[0008] Register a listening event in the C language application layer of the VPN tunnel, listen to the event data traffic packets sent by the virtual network card based on the listening event, and parse the event data traffic packets to extract protocol fields;
[0009] Determine a protocol parsing module based on the protocol fields, and extract the target IP address in the event data traffic packets through the protocol parsing module;
[0010] Determine the system network path that the network session needs to be bound to based on the target IP address, and distribute the event data traffic packet to the cellular data network or the Wi-Fi network of the IoT device based on the system network path bound by the network session.
[0011] Optionally, register a listening event in the C language application layer of the VPN tunnel, and listen to the event data traffic packet sent by the virtual network card based on the listening event, including the following steps:
[0012] Detect the original data traffic packet sent by the Linux kernel layer through the virtual network card, and determine whether the original data traffic packet is an event data traffic packet corresponding to the listening event according to the file descriptor in the original data traffic packet;
[0013] If so, read the original data traffic packet as an event data traffic packet, otherwise, skip the reading of the original data traffic packet.
[0014] Optionally, parse the event data traffic packet and extract the protocol field, including the following steps:
[0015] Extract the first byte of the event data traffic packet to determine the IP version of the event data traffic packet, where the IP version includes IPv4 version and IPv6 version;
[0016] When the event data traffic packet is of IPv4 version, parse the header information using the standard IPv4 header structure, and locate the protocol field based on the header information to extract the protocol field;
[0017] When the event data traffic packet is of IPv6 version, parse the basic header, and traverse the existing extension headers to determine the final upper-layer protocol, and extract the protocol field.
[0018] Optionally, determine the system network path that the network session needs to be bound to based on the target IP address, including the following steps:
[0019] Set the address range for identifying the IoT device, and determine whether the target IP address is within the address range;
[0020] If the target IP address is within the address range, bind the network session to the Wi-Fi network corresponding to the IoT device, otherwise bind the network session to the cellular network.
[0021] Optionally, establish a VPN tunnel and configure a virtual network card for intercepting and forwarding event data traffic packets, including the following steps:
[0022] Set the basic parameters for VPN construction and establish a virtual network card. Among them, the basic parameters include VPN session name, MTU, IP address, routing, and DNS;
[0023] Pass the file descriptor of the virtual network card to the C language application layer through the application layer of the VPN, and configure the data path with the virtual network card in the C language application layer based on the file descriptor of the virtual network card.
[0024] Optionally, register a listening event, including the following steps:
[0025] Register the file descriptor of the virtual network card and the socket file descriptor corresponding to the network session to be listened to, and generate a listening event.
[0026] Optionally, determine the protocol parsing module based on the protocol field, including the following steps:
[0027] According to the protocol field, call the TCP protocol parsing module, UDP protocol parsing module, or ICMP protocol parsing module corresponding to the protocol field.
[0028] Optionally, it further includes the following steps:
[0029] Record each network session and store the connection status, active time, and data transfer statistics of each network session.
[0030] Optionally, it further includes the following steps:
[0031] Record the processed listening data traffic packets in PCAP format, and output debug information and error logs using the system log interface at the same time.
[0032] A network data traffic distribution system, the network data traffic distribution system executes the network data traffic distribution method described in any one of the above, including:
[0033] An application layer, which is set inside the VPN tunnel and is used to receive the event data traffic packets from the C language application layer, determine the system network path that the network session needs to bind based on the protocol field and the destination IP address, and distribute the event data traffic packets to the cellular data network or the Wi-Fi network of the IoT device based on the system network path bound by the network session;
[0034] A C language application layer, which is set inside the VPN tunnel and is used to receive and parse the event data traffic packets transmitted by the virtual network card, and upload them to the application layer after extracting the protocol field and the destination IP address of the event data traffic packets;
[0035] The Linux kernel layer is used to monitor raw data traffic packets and send the event data traffic packets in the raw traffic packets to the C language application layer through a virtual network card;
[0036] The virtual network card is used to intercept and forward event data traffic packets.
[0037] Adopting the technical solution provided by the present invention, compared with the prior art, it has the following beneficial effects:
[0038] Through the construction of a VPN tunnel, full traffic interception is achieved, and the event-driven mechanism of the C language application layer is used to parse, classify, and dynamically distribute event data traffic packets, realizing the two-way parallel use of IoT devices and the Internet; the Java layer and the C layer work closely together. The Java layer is responsible for the initialization configuration of the VPN tunnel, resource management, and the implementation of the network interface selection strategy, while the C layer is responsible for packet capture, protocol parsing, event handling, and session state management. The two work together to ensure an efficient, stable, and intelligent traffic diversion scheme in a multi-network environment. Description of the Drawings
[0039] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0040] Figure 1 It is a flowchart of a network data traffic distribution method proposed in Embodiment 1;
[0041] Figure 2 It is an architecture diagram of a network data traffic distribution system proposed in Embodiment 2. Detailed Embodiments
[0042] The following further elaborates on the present invention in conjunction with embodiments. The following embodiments are explanations of the present invention, and the present invention is not limited to the following embodiments.
[0043] Embodiment 1
[0044] As Figure 1As shown in the figure, a network data traffic distribution method includes the following steps: First, establish a VPN tunnel and configure a virtual network card for intercepting and forwarding event data traffic packets. Specifically, set the basic parameters for VPN construction and establish a virtual network card. The basic parameters include the VPN session name, MTU, IP address, routing, and DNS. Pass the file descriptor of the virtual network card to the C language application layer through the application layer of the VPN, and configure the data path with the virtual network card in the C language application layer based on the file descriptor of the virtual network card.
[0045] When the present invention distributes data traffic, it mainly realizes the distribution of data traffic through the VPN tunnel. Therefore, it is necessary to build a VPN tunnel in the mobile terminal and establish a connection between the VPN tunnel and the internal system of the mobile terminal, so as to realize the monitoring and distribution of traffic data. Among them, the interactive display form of the VPN tunnel set in the mobile terminal can be in the form of a switch (such as the UI display form of a Bluetooth switch), or it can be displayed in the form of a software APP.
[0046] When building a VPN tunnel in the mobile terminal, first start and initialize the VPN tunnel, then the application layer calls the VpnService API provided by the mobile terminal system, and then configures the necessary parameters for the VPN connection through the Builder class of the mobile terminal system, such as the VPN session name, MTU, IP address, routing, and DNS, etc. Then call the system's establish() function to establish a virtual network card and obtain the file descriptor of the virtual network card (also known as the TUN interface). Then the application layer passes the file descriptor of the virtual network card to the C language application layer through JNI to ensure that the C language application layer configures the virtual network card for the C language application layer to communicate with the Linux kernel layer in the mobile terminal system, and makes basic preparations for subsequent packet transmission and processing.
[0047] It should be noted that the application layer is responsible for high-level network policies and traffic splitting decisions. By obtaining the socket file descriptor and the target IP address from the C layer, it judges the traffic attribution (for example, whether it is a local area network device such as a dash cam), and calls the system API (such as network.bindSocket(FileDescriptor fd)) to implement the binding and distribution of specific network interfaces; the C layer realizes high-speed packet processing and protocol parsing based on JNI, mainly using an event-driven mechanism (such as epoll) to loop and read packets between the TUN interface and various sockets, and performs protocol parsing, verification, and forwarding of the packets to provide underlying support for the Java layer.
[0048] After the establishment of the VPN tunnel and the configuration of the virtual network card are completed, it is necessary to register a listening event in the C language application layer of the VPN tunnel, and listen to the event data traffic packets sent by the virtual network card based on the listening event, so as to determine the event data traffic packets that need to be distributed through the VPN tunnel from all the monitored original data traffic packets in the Linux kernel layer of the mobile system, and then parse the event data traffic packets to extract the protocol fields.
[0049] Specifically, the registration of the listening event is required first, including the following steps: The C language application layer uses the epoll mechanism provided by the Linux kernel layer to create a listening instance, registers the file descriptor of the virtual network card and the socket file descriptor corresponding to the network session to be monitored, generates a listening event, and the listening event mainly includes EPOLLIN and EPOLLERR events.
[0050] Then, the event data traffic packets sent by the virtual network card are listened to based on the listening event, which specifically includes the following steps: Detect the original data traffic packets sent by the Linux kernel layer through the virtual network card, and judge whether the original data traffic packet is an event data traffic packet corresponding to the listening event according to the file descriptor in the original data traffic packet; if so, read the original data traffic packet as an event data traffic packet, otherwise, skip the reading of the original data traffic packet.
[0051] In the main loop, the epoll_wait() function is used to detect whether the events on the file descriptors of each original data traffic packet are triggered. Once readable data is detected on the TUN interface (that is, it is determined that the file descriptor in the original data traffic packet is the same as the file descriptor required by the listening event, which is judged as readable data and belongs to the event data traffic packet), the read function is immediately called to obtain the event data traffic packet into the buffer area, and then the parsing step can be carried out.
[0052] Parse the event data traffic packet and extract the protocol field, including the following steps: Extract the first byte of the event data traffic packet to judge the IP version of the event data traffic packet, where the IP version includes IPv4 version and IPv6 version; when the event data traffic packet is of IPv4 version, the standard IPv4 header structure is used to parse the header information, and the protocol field is located based on the header information to extract the protocol field; when the event data traffic packet is of IPv6 version, the basic header is parsed, and the existing extension headers are traversed to determine the final upper-layer protocol, and the protocol field is extracted.
[0053] Specifically, the read event data traffic packet is first processed by the IP parsing module. The IP version is determined based on the first byte of the event data traffic packet. For IPv4 data packets, the IPv4 header structure is used to parse the header information, and the position of the protocol field is found according to the header information to obtain the protocol field. In addition, the headerlength, total length, and checksum can also be checked, and the standard checksum algorithm (such as using 16-bit one's complement summation) is used to verify the data integrity; for IPv6 data packets, in addition to parsing the basic header (struct ip6_hdr), it is also necessary to traverse the possible extension headers, and the upper-layer protocol is gradually located by checking the Next Header field, so as to obtain the position of the protocol field and extract the protocol field.
[0054] Thus, protocol parsing is performed in a corresponding manner according to different versions of the event data volume traffic packet to determine which protocol is used to extract the target IP address, that is: determining the protocol parsing module based on the protocol field, including the following steps: according to the protocol field, calling the TCP protocol parsing module, UDP protocol parsing module, or ICMP protocol parsing module corresponding to the protocol field. For example, when it is determined that the version of the event data traffic packet is the IPv4 version, and the protocol field obtained after parsing the header information using the standard structure is the TCP protocol, then the TCP protocol parsing module is called to extract the target IP address from the event data traffic packet.
[0055] It should be noted that when extracting the target IP address, it can be performed in the C language application layer or in the application layer. Therefore, after obtaining the target IP address, the system network path to which the network session needs to be bound can be determined based on the target IP address, and the event data traffic packet can be distributed to the cellular data network or the Wi-Fi network of the IoT device based on the system network path bound by the network session.
[0056] Among them, determining the system network path to which the network session needs to be bound based on the target IP address includes the following steps: setting an address range for identifying the IoT device, and determining whether the target IP address is within the address range; if the target IP address is within the address range, then binding the network session to the Wi-Fi network corresponding to the IoT device, otherwise binding the network session to the cellular network.
[0057] For example, when the IoT device is a dash cam and the mobile device is an Android phone, when the Android phone connects to the dash cam, data transmission is achieved through the Wi-Fi network of the dash cam. However, when there is an access request from WeChat on the Android phone, its data traffic cannot be transmitted through the Wi-Fi network connected to the dash cam. Therefore, the traditional method requires the user to turn off the Wi-Fi network connection between the Android phone and the dash cam, and then the data transmission of the access request of WeChat can be achieved through the mobile data of the Android phone. Based on the network data traffic distribution method of the present invention, the target IP address of the event data traffic packet for access can be extracted through the VPN tunnel, and it can be determined whether the event data traffic packet belongs to the dash cam or the WeChat request according to the target IP address, and different network channels are selected for data traffic transmission and distribution according to different request parties, so that there is no need for the user to manually adjust, achieving the effect of automatically selecting the distribution channel and solving the problem of the two-way parallel use of IoT devices and the Internet.
[0058] On the other hand, the present invention further includes the following steps: recording each network session and storing the connection status, active time, and data transmission statistical information of each network session.
[0059] Specifically, the present invention also has the ability of real-time monitoring. All active network sessions (whether TCP, UDP, or ICMP) are recorded and managed in the C language application layer. The system regularly checks the last active time, data transmission situation, and connection status of the session, and uses system calls such as getsockopt() and ioctl() to detect the socket status and window information; at the same time, it also has the function of timeout and exception handling. For sessions that exceed the preset timeout time or encounter errors, the system calls API such as the close function to close the connection and recycle memory resources. The session management module will also adjust the events registered by epoll to ensure efficient response.
[0060] The present invention records the monitored data traffic packets in PCAP format, and at the same time uses the system log interface to output debug information and error logs. Specifically, to support debugging and performance analysis, some processed data packets will be recorded in a file in PCAP format, and standard file I / O APIs (such as fwrite, ftruncate, lseek, etc.) are used to implement data writing; the system uses the operating system log interface (such as android_log_print of Android) in both the Java layer and the C layer to output detailed debug information, error logs, and status information, which is convenient for problem tracking and subsequent optimization.
[0061] Embodiment 2
[0062] Such as Figure 2As shown in the figure, a network data traffic distribution system includes:
[0063] The application layer is arranged inside the VPN tunnel and is used to receive the event data traffic packets of the C language application layer, determine the system network path that the network session needs to bind based on the protocol field and the destination IP address, and distribute the event data traffic packets to the cellular data network or the Wi-Fi network of the IoT device based on the system network path bound by the network session;
[0064] The C language application layer is arranged inside the VPN tunnel and is used to receive and parse the event data traffic packets transmitted by the virtual network card, and upload them to the application layer after extracting the protocol field and the destination IP address of the event data traffic packets;
[0065] The Linux kernel layer is used to monitor the raw data traffic packets and send the event data traffic packets in the raw traffic data packets to the C language application layer through the virtual network card;
[0066] The virtual network card is used to intercept and forward the event data traffic packets.
[0067] Since the network data traffic distribution system of this embodiment executes the network data traffic distribution method shown in Embodiment 1, it will not be repeated in this embodiment.
[0068] The above is only a preferred embodiment of the present invention, and it does not impose any formal or substantial limitations on the present invention. It should be noted that for those of ordinary skill in the art in this technical field, without departing from the method of the present invention, several improvements and supplements can still be made, and these improvements and supplements should also be regarded as the protection scope of the present invention. For those who are familiar with this professional technology, without departing from the spirit and scope of the present invention, any equivalent changes made by using the technical content disclosed above, such as minor modifications, decorations, and evolutions, are all equivalent embodiments of the present invention; at the same time, any equivalent changes, modifications, and evolutions made to the above embodiments based on the essence of the present invention still fall within the scope of the technical solution of the present invention.
Claims
1. A method for distributing network data traffic, characterized in that, Including the following steps: Establish a VPN tunnel and configure a virtual network card for intercepting and forwarding event data packets; Register a listening event at the C language application layer of the VPN tunnel, listen for the event data packets sent by the virtual network card based on the listening event, and parse the event data packets to extract protocol fields; Determine a protocol parsing module based on the protocol fields, and extract the target IP address in the event data packets through the protocol parsing module; Determine the system network path that the network session needs to bind based on the target IP address, and distribute the event data packets to the cellular data network or the Wi-Fi network of the IoT device based on the system network path bound by the network session.
2. The network data traffic distribution method according to claim 1, characterized in that Register a listening event at the C language application layer of the VPN tunnel, and listen for the event data packets sent by the virtual network card based on the listening event, including the following steps: Detect the raw data packets sent by the Linux kernel layer through the virtual network card, and determine whether the raw data packets are event data packets corresponding to the listening event according to the file descriptors in the raw data packets; If so, read the raw data packets as event data packets, otherwise, skip the reading of the raw data packets.
3. A method for distributing network data traffic according to claim 1, characterized in that Parse the event data packets to extract protocol fields, including the following steps: Extract the first byte of the event data packet to determine the IP version of the event data packet, where the IP version includes IPv4 version and IPv6 version; When the event data packet is of IPv4 version, parse the header information using the standard IPv4 header structure, and locate the protocol fields based on the header information to extract the protocol fields; When the event data packet is of IPv6 version, parse the basic header, and traverse the existing extension headers to determine the final upper-layer protocol and extract the protocol fields.
4. A method for distributing network data traffic according to claim 1, characterized in that, Determine the system network path that the network session needs to bind based on the target IP address, including the following steps: Set an address range for identifying IoT devices, and determine whether the target IP address is within the address range; If the target IP address is within the address range, bind the network session to the Wi-Fi network corresponding to the IoT device, otherwise bind the network session to the cellular network.
5. A method for distributing network data traffic according to claim 1, characterized in that, Establish a VPN tunnel and configure a virtual network card for intercepting and forwarding event data packets, including the following steps: Set the basic parameters for VPN construction and establish a virtual network card, where the basic parameters include VPN session name, MTU, IP address, routing, and DNS; Pass the file descriptor of the virtual network card to the C language application layer through the application layer of the VPN, and configure the data path with the virtual network card in the C language application layer based on the file descriptor of the virtual network card.
6. A method for distributing network data traffic according to claim 2, characterized in that, Register a listening event, including the following steps: Register the file descriptor of the virtual network card and the socket file descriptor corresponding to the network session to be listened to, and generate a listening event.
7. A method for distributing network data traffic according to claim 3, characterized in that Determine a protocol parsing module based on the protocol fields, including the following steps: According to the protocol field, call the TCP protocol parsing module, UDP protocol parsing module or ICMP protocol parsing module corresponding to the protocol field.
8. A method for distributing network data traffic according to any one of claims 1-7, characterized in that, It further includes the following steps: Record each network session and store the connection status, active time, and data transmission statistics of each network session.
9. A method for distributing network data traffic according to any one of claims 1-7, characterized in that, It further includes the following steps: Record the processed monitored data traffic packets in PCAP format, and output debug information and error logs using the system log interface.
10. A network data traffic distribution system, characterized in that, The network data traffic distribution system executes the network data traffic distribution method according to any one of claims 1-9, including: The application layer, which is set inside the VPN tunnel, is used to receive the event data traffic packets of the C language application layer, determine the system network path that the network session needs to bind based on the protocol field and the destination IP address, and distribute the event data traffic packets to the cellular data network or the Wi-Fi network of the IoT device based on the system network path bound by the network session; The C language application layer, which is set inside the VPN tunnel, is used to receive and parse the event data traffic packets transmitted by the virtual network card, and upload them to the application layer after extracting the protocol field and the destination IP address of the event data traffic packets; The Linux kernel layer is used to monitor the raw data traffic packets and send the event data traffic packets in the raw traffic packets to the C language application layer through the virtual network card; The virtual network card is used to intercept and forward the event data traffic packets.
Citation Information
Patent Citations
Multiple tunnel concurrent model implementation method based on virtual network card technology
CN101626337A
Real-time network data capture method based on connection
CN101815014A
Method and system for transmitting files in concurrent mode
CN103441996A
Data distribution method and apparatus, computer device and storage medium
CN108111619A
Data transmission methods and related products
CN109526020A