Data communication protocol implementation method, apparatus, and system
The network IO library functions of the application are taken over through dynamic library injection technology and replaced the initial protocol based on these hooks, solving the problem of opaque network communication protocol replacement in the prior art, realizing unconscious protocol replacement.
Patent Information
- Application Number
- CN202310942784.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-28
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2043-07-28
AI Technical Summary
The prior art is difficult to realize transparent replacement of network communication protocols, and cannot replace the network communication protocol of the application without perception, resulting in compatibility problems.
By injecting the target dynamic library into the target application, it takes over the network IO-related library functions and based on these library function hooks, a new protocol is used to replace the target application's initial protocol.
It realizes transparent replacement of the application communication protocol, without modifying code or recompiling the target application, and has no awareness of the user.
Smart Images

Figure CN119172446B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to the field of communication technology, and particularly to a method, apparatus, and system for implementing a data communication protocol. Background Art
[0002] In the prior art, replacing the network communication protocol of an application requires modifying the application source code. Moreover, once the original network communication protocol of the application is replaced, it is difficult to make other applications that follow the old communication protocol compatible with this application simultaneously, and it is impossible to achieve the "transparent" replacement effect of the network communication protocol, that is, without modifying the code or recompiling the target application, and being imperceptible to users. Summary of the Invention
[0003] Embodiments of this application provide a method, apparatus, system, device, and storage medium for implementing a data communication protocol.
[0004] According to a first aspect, embodiments of this application provide a method for implementing a data communication protocol, the method including: injecting a target dynamic library into a target application to take over network IO-related library functions; and replacing an initial protocol of the target application with a new protocol based on network IO-related library function hooks.
[0005] According to a second aspect, embodiments of this application provide a device for implementing a data communication protocol, the device including: an injection module configured to inject a target dynamic library into a target application to take over network IO-related library functions; and a replacement module configured to replace an initial protocol of the target application with a new protocol based on network IO-related library function hooks.
[0006] According to a third aspect, embodiments of this application provide a system for implementing a data communication protocol, the system including: a first communication entity and a second communication entity that communicate with each other based on an initial protocol; the first communication entity is configured to inject a first dynamic library into a first application to take over network IO-related library functions; and replace an initial protocol of the first application with a new protocol based on network IO-related library function hooks; the second communication entity is configured to inject a second dynamic library into a second application to take over network IO-related library functions; and replace an initial protocol of the second application with a new protocol based on network IO-related library function hooks.
[0007] According to a fourth aspect, embodiments of this application provide an electronic device, the electronic device including one or more processors; a storage device having stored thereon one or more programs, when the one or more programs are executed by the one or more processors, enabling the one or more processors to implement the method for implementing a data communication protocol according to any one of the embodiments of the first aspect.
[0008] According to a fifth aspect, an embodiment of the present application provides a computer-readable medium, on which a computer program is stored, and when the program is executed by a processor, it implements the data communication protocol implementation method according to any embodiment of the first aspect.
[0009] The present application provides a data communication protocol implementation system, which includes a first communication entity and a second communication entity that communicate with each other based on an initial protocol; the first communication entity is used to inject a first dynamic library into a first application program to take over network IO-related library functions; based on network IO-related library function hooks, a new protocol is used to replace the initial protocol of the first application program; the second communication entity is used to inject a second dynamic library into a second application program to take over network IO-related library functions; based on network IO-related library function hooks, a new protocol is used to replace the initial protocol of the second application program, that is, a transparent replacement of the application program communication protocol is achieved through dynamic library injection.
[0010] It should be understood that the content described in this part is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it used to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] Figure 1a is an architecture diagram of an embodiment of a data communication protocol implementation system according to the present application;
[0012] Figure 1b is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0013] Figure 1c is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0014] Figure 1d is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0015] Figure 1e is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0016] Figure 1f is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0017] Figure 1g is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0018] Figure 1h is a flowchart of an embodiment of a data communication protocol implementation system according to the present application;
[0019] Figure 1i It is a flowchart of an embodiment of a system implemented according to the data communication protocol of the present application;
[0020] Figure 1j It is a flowchart of an embodiment of a system implemented according to the data communication protocol of the present application;
[0021] Figure 1k It is a flowchart of an application scenario of a system implemented according to the data communication protocol of the present application;
[0022] Figure 2 It is an architecture diagram of an application scenario of a system implemented according to the data communication protocol of the present application;
[0023] Figure 3 It is a flowchart of an embodiment of a method for implementing a data communication protocol according to the present application;
[0024] Figure 4 It is a flowchart of an embodiment of a data communication implementation device according to the present application;
[0025] Figure 5 It is a schematic structural diagram of a computer system of a server suitable for implementing the embodiments of the present application. Detailed implementation manners
[0026] The following describes exemplary embodiments of the present application with reference to the accompanying drawings. Various details of the embodiments of the present application are included to facilitate understanding, and they should be considered merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0027] It should be noted that, without conflict, the embodiments in the present application and the features in the embodiments can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in conjunction with the embodiments.
[0028] Figure 1a An architecture 100 of a data communication protocol implementation system that can be applied to the present application is shown. In this embodiment, the data communication protocol implementation system includes a first communication entity and a second communication entity that communicate with each other based on an initial protocol.
[0029] The first communication entity and the second communication entity can be two communication entities that communicate according to any communication protocol (for example, connection-oriented protocol, connectionless protocol, data stream protocol, datagram protocol, reliable protocol, unreliable protocol, etc.).
[0030] Among them, the first communication entity can inject the first dynamic library into the first application to take over the library functions related to network I / O, and replace the initial protocol of the first application with a new protocol according to the hooks of the library functions related to network I / O or the callback interfaces of the hooks of the library functions related to network I / O. That is, the initial protocol is replaced with a new protocol by means of dynamic library injection.
[0031] The second communication entity injects the second dynamic library into the second application to take over the library functions related to network I / O, and replace the initial protocol of the second application with a new protocol according to the hooks of the library functions related to network I / O or the callback interfaces of the hooks of the library functions related to network I / O. That is, the initial protocol is replaced with a new protocol by means of dynamic library injection.
[0032] Among them, the first dynamic library and the second dynamic library are the same.
[0033] After replacing the new protocol, the first communication entity and the second communication entity can communicate based on the new protocol.
[0034] Here, for connection-oriented protocols, the library functions related to I / O can be socket, bind, listen, accept, recv, send, close; for connectionless protocols, the library functions related to I / O can be recv and send, sendmsg and recvmsg, etc.
[0035] Among them, the new protocol and the initial protocol can both be connection-oriented protocols, for example, TCP protocol, etc.; the new protocol and the initial protocol can also both be connectionless protocols, for example, UDP protocol, etc.; the new protocol and the initial protocol can both be stream protocols or datagram protocols; the new protocol and the initial protocol can also both be reliable protocols or unreliable protocols. This application does not make any limitations in this regard.
[0036] Here, dynamic library injection is used to indicate that by loading a dynamic library into an application, the code in the dynamic library is injected into the target application, so that this part of the code is executed by the application, or the original code in the application is dynamically replaced with the injected code (the injected code is also called code "hook" - hook).
[0037] The dynamic library injected into the application can either override the code of the application itself or override the code of the dynamic library on which the application depends. This application does not make any limitations in this regard.
[0038] Specifically, in the Linux system, the dynamic library to be preloaded when starting the application can be set by specifying the environment variable "LD_PRELOAD"; when the client or server application starts, the specified dynamic library will be loaded into the memory address space of the process before the application is executed. The relevant code is as follows:
[0039] Server
[0040] LD_PRELOAD= / my / dir / newprotocol.so server
[0041] Client
[0042] LD_PRELOAD= / my / dir / newprotocol.so client
[0043] Here, when the execution entity starts the application, there are various specific ways to inject the dynamic library into the application. For example, there are inline injection, overlay injection, etc.
[0044] Among them, inline injection is used to indicate directly injecting the code into the original code position to "hijack" the original code; overlay injection is used to indicate redirecting the function pointer in the function jump table or symbol table by overwriting it to the target function.
[0045] Here, the object of overlay injection can be the application itself or the library functions that the application depends on.
[0046] In some alternative ways, the first communication entity is used to execute the library functions of the new protocol of the client when initiating a connection; the second communication entity is used to execute the library functions of the new protocol of the server when listening for connection establishment.
[0047] In this implementation, the new protocol is a communication protocol for the client / server architecture, that is, the communication protocol of the client / server architecture, that is, the communication protocol of the CS architecture. The first communication entity is the client, and the second communication entity is the server; the first communication entity is used to execute the library functions of the new protocol of the client when initiating a connection; the second communication entity is used to execute the library functions of the new protocol of the server when listening for connection establishment.
[0048] Specifically, when the initial network protocol is based on listening for connection establishment, by overloading the bind, listen, and accept functions on the server side and overloading the connect function on the client side, the replacement of the listening and connection establishment operations is achieved.
[0049] After listening for connection establishment, the server and the client perform data transmission based on the overloaded send and recv functions.
[0050] This implementation realizes the connection establishment function between the first communication entity and the second communication entity that follows the communication protocol of the client / server architecture by the first communication entity executing the library functions of the new protocol of the client when initiating a connection and the second communication entity executing the library functions of the new protocol of the server when listening for connection establishment.
[0051] In some alternative ways, the server is used to simultaneously listen for connection establishment requests of the original protocol and the new protocol, and perform data transmission based on the new protocol or the initial protocol.
[0052] In this implementation, the server can simultaneously listen for connection establishment requests of the original protocol and the new protocol. If a connection establishment request of the new protocol is detected, data transmission is performed based on the new protocol. If a connection establishment request of the initial protocol is detected, data transmission is performed based on the initial protocol, thus achieving compatibility for the two types of listening and connection establishment operations.
[0053] In some alternative ways, connection establishment requests of the original protocol and the new protocol are simultaneously listened for, and data transmission is performed based on the new protocol or the initial protocol. Specifically, it includes: configuring a listening specified context structure. For the listening specified context structure, in response to determining that there is a listening socket indicating that a new connection request has arrived, a target specified context structure corresponding to the listening socket is created, and the address and port of the peer are filled in the first address field in the target specified context structure; in response to determining that the target specified context structure is a new protocol specified context structure and the send or receive status is ready, data transmission is performed with the client based on the address and port of the peer in the first address field.
[0054] In this implementation, for a communication protocol where the new protocol is a client / server architecture, the execution entity can pre-configure the listening specified context structure based on the callback interface of the network IO related library function hooks.
[0055] Among them, the listening specified context structure includes a new protocol listening socket, an initial protocol listening socket, and a second address. The second address (such as sock_addr) is used to store the bound address and the listening port.
[0056] Furthermore, in response to determining that there is a listening socket in the new protocol listening socket or the initial protocol listening socket indicating that a new connection request has arrived, a target specified context structure corresponding to the listening socket is created, and the address and port of the peer are filled in the first address field (such as peer_addr) in the target specified context structure.
[0057] Among them, the target specified context structure is a new protocol specified context structure or an initial protocol specified context structure.
[0058] Here, the monitored specified context structure, the new protocol specified context structure, and the initial protocol specified context structure are different types of the specified context structure. The monitored specified context structure, the new protocol specified context structure, and the initial protocol specified context structure have the same data structure as the specified context structure but use different fields. The monitored specified context structure, the new protocol specified context structure, and the initial protocol specified context structure are respectively used to maintain different types of socket descriptors.
[0059] Among them, the specified context structure, for example, the context structure named "SockFd", may include: FD (used to store the socket file descriptor), flags: used to store the status flags of the socket (such as: NON_BLOCKING, etc.), the first address field (used to store the peer address and port of the established connection socket), the new protocol monitored socket field (used to store the new protocol monitored socket), the initial protocol monitored socket field (used to store the new protocol monitored socket), the second address field (used to store the bound address and the listening port), specifically as Figure 1b shown.
[0060] Here, it should be noted that the new protocol can be any communication protocol of the client / server architecture, for example, the TCP protocol, etc. This application does not make any limitations in this regard.
[0061] In addition, if the new protocol is the KCP protocol using the UDP link as the transport layer, the monitored socket is the UDP monitored socket, that is, the UDP listen socket. The specified context structure may further include a KCP field: used to store the KCP connection context pointer. Specifically, if the new protocol monitored socket is the UDP listen socket, after the execution entity fills the second address field, it also needs to create a KCP context for it and bind it to the KCP ID in the connection establishment request packet, so as to realize the pairing of KCP communication.
[0062] Furthermore, if the new protocol is the KCP protocol and the initial protocol is the TCP protocol, both are connection-oriented protocols. The functions related to KCP protocol communication are socket, bind, listen, accept, recv, send, close. The server can create a socket through the socket function; take over the binding operation of the listening port through the callback interface of the bind function hook; take over the listening operation through the callback interface of the listen function hook; take over the operation of accepting a new connection request from the monitored socket through the callback interface of the accept function hook; take over the operation of the client initiating a connection through the callback interface of the connect library function hook.
[0063] Specifically, as Figure 1c shown, in the callback of the library function socket hook, it is judged whether the socket type created by the application request is the initial protocol type of the application, such as TCP. If so, the system call SYS_socket is executed to create a socket descriptor, and a specified context structure is created with this socket descriptor as the key, and the specified context structure is stored in the SockFdtable, that is, the specified context structure table; if not, the control is returned to the system native library function and operated according to the operation logic of the native library function.
[0064] Furthermore, as Figure 1d shown, in the callback of the library function bind hook, the socket descriptor is searched in the SockFd table. If found, an initial protocol listening socket, that is, a TCP listening socket, is created, and the system call SYS_bind is executed to bind the initial protocol address and port. If the binding fails, an error code is returned and error is processed. If the binding is successful, a new protocol listening socket, that is, a UDP listening socket, is created, and the system call SYS_bind is executed to bind the new protocol port. If the binding is successful, the second address field sock_addr of the specified context structure SockFd is set with the incoming second address sock_addr to obtain the listening specified context structure. If the binding fails, an error code is returned and error is processed.
[0065] Furthermore, as Figure 1eAs shown in the figure, in the callback of the accept hook of the library function, the socket descriptor is searched in the SockFd table. If found, the POLLIN status of the TCP listening socket is judged, and whether there is a new connection is determined. If so, the system call SYS_accept is executed to create a server-side TCP socket, and whether the TCP connection establishment is successful is judged. If it is, a TCP specified context structure corresponding to the TCP listen socket, that is, a TCP SockFd, is created and stored in the SockFd table, and the socket FD of the server-side TCP connection is returned. If the TCP connection establishment fails, an error code is returned and the error is processed. If there is no new connection, the POLLIN status of the UDP listening socket is judged, and whether there is a new connection request packet is determined. If so, the system call SYS_recv is executed to receive the connection request packet from the UDP listening socket, and whether the connection request packet is valid is judged. If it is valid, the system call SYS_socket is executed to create a server-side UDP socket and a SockFd, create a KCP context for the SockFd, set KCP->kcp_id = the connection request ID, that is, create a UDP specified context structure, that is, a UDP SockFD. Further, whether the KCP connection establishment is successful is judged. If it is successful, the UDP SockFD is stored in the SockFd table, and the UDP socket FD of the newly established KCP connection is returned. If it fails, an error code is returned and the error is processed. If there is no new connection request packet or the connection request packet is invalid, it is returned that the newly established connection is not ready.
[0066] In addition, for the listen library function, its callback process is as Figure 1f shown. Specifically, the socket descriptor is searched in the SockFd table. If found, the SYS_listen system call is called for the TCP listen socket in the SockFd, and whether the Listen is successful is judged. If it is successful, the operation success is returned. If it fails, an error code is returned and the error is processed.
[0067] Further, if the target specified context structure is a new protocol specified context structure, and the sending status or the receiving status is ready, that is, there is free space in the sending buffer or the data received has been cached in the receiving buffer, that is, POLLOUT is valid or POLLIN is valid, then the send library function hook and the recv library function hook can be further used to send and receive data with the client according to the address and port of the peer recorded in the first address field.
[0068] Specifically, taking the new protocol as the KCP protocol using the UDP link as the transport layer and the initial protocol as the TCP protocol as an example, asFigure 1g As shown, in the callback of the library function send hook, look up the socket descriptor in the SockFd table. If not found, transfer control to the native library function. If found, check the KCP connection status and determine whether KCP is not connected or has been closed. If so, return an error code and handle the error. If not, call the KCP send interface. Further, determine whether the KCP send window is full. If so, return an error code and handle the error. If not, return the actual number of bytes sent.
[0069] Another example Figure 1h As shown, in the callback of the library function recv hook, look up the socket descriptor in the SockFd table. If not found, transfer control to the native library function. If found, check the KCP connection status and determine whether KCP is not connected. If so, return an error code and handle the error. If not, further determine whether KCP has been closed. If so, return 0 indicating that the peer has closed. If not, call the KCP receive data interface and determine whether there is no data in the KCP receive buffer. If so, return an error code and handle the error. If not, return the actual number of bytes received.
[0070] In addition, after the data transmission is completed, it is also necessary to close the communication based on the hooks of the network IO related library functions.
[0071] Specifically, as Figure 1i shown, the initial protocol is the TCP protocol, and the target protocol is the KCP protocol using the UDP link as the transport layer. In the callback of the library function close hook, look up the socket descriptor in the SockFd table. If not found, transfer control to the native library function. If found, delete the key value in the SockFd table and determine whether the type of the SockFD, that is, the specified context structure, is of the UDP type. If so, close the KCP connection, release the SockFd and execute the system call SYS_close to close the socket descriptor. If the SockFD is not of the UDP type, directly release the SockFd and execute the system call SYS_close to close the socket descriptor.
[0072] This implementation method configures a specified context structure. For listening to the specified context structure, in response to determining that there is a listening socket indicating that a new connection request has arrived, a target specified context structure corresponding to the listening socket is created, and the address and port of the peer are filled in the first address field in the target specified context structure. In response to determining that the target specified context structure is a new protocol specified context structure and the send or receive status is ready, data transmission is performed with the client based on the address and port of the peer in the first address field, thereby implementing support for the initial protocol and the new protocol.
[0073] In some alternative ways, the server configures a callback interface for the library function to listen for whether the specified context structure indicates that a new connection request has arrived, whether the receive status of the new protocol specified context structure is ready, and whether the send status is ready.
[0074] In this implementation method, the server can configure a callback interface to detect whether the specified context structure for listening indicates that a new connection request has arrived, i.e., POLLIN is valid, whether the receive status of the new protocol specified context structure is ready, i.e., POLLIN is valid, and whether the send status is ready, i.e., POLLOUT is valid, that is, to detect the poll status for the library function hooks such as poll / select / epoll_wait to call.
[0075] Specifically, as Figure 1j shown, taking the initial protocol of the application as the TCP protocol and the new protocol as the UDP protocol as an example, look up the socket descriptor in the SockFd table. If found, further determine the type of the specified context structure corresponding to the socket descriptor, i.e., the SockFd type. If it is a listening specified context structure, determine whether the TCP listen socket is ready. If so, return POLLIN. If not, determine whether the UDP listen socket is readable. If so, return POLLIN. If not, return 0 indicating that there are no ready events. Further, if the type of the specified context structure is a new protocol specified context structure, i.e., the UDP specified context structure, determine whether KCP is not opened or has been closed. If so, return an error code and handle the error. If not, call the KCP interface to check the send / receive window status and determine whether both the send / receive windows are not ready. If so, return 0 indicating that there are no ready events. If not, return the POLLIN status or the POLLOUT status. If the type of the specified context structure is an initial protocol specified context structure, i.e., the TCP specified context structure, return the control.
[0076] This implementation method improves the timeliness of detecting connection establishment requests by configuring callback interfaces for library function calls to monitor whether a specified context structure indicates the arrival of a new connection request, whether the receiving status of the new protocol specified context structure is ready, and whether the sending status is ready.
[0077] In some optional ways, the server is further configured to, in response to determining that the new protocol is a communication protocol for a client / server architecture, receive a connection establishment request sent by a client of the new protocol based on a network IO related library function hook.
[0078] In this embodiment, if the new protocol is a communication protocol for a client / server architecture, the server can receive a connection establishment request sent by a client of the new protocol based on the callback interface of a network IO related library function hook, that is, receive a connection establishment request from a client started in a dynamic injection manner.
[0079] Here, a specified dynamic library can be pre-injected into the client application to take over network IO related library functions, such as the connect library function. Further, a connection establishment request packet of the new protocol is sent to the new protocol listening port of the server through the callback interface of the network IO related library function hook to initiate a new protocol connection establishment request.
[0080] Specifically, as Figure 1k shown, the new protocol is the KCP protocol. The callback process of the connect library function hook in the KCP protocol client is as follows: search for the socket descriptor in the client SockFd table. If found, allocate a new KCP connection ID, create a KCP context, and set KCP->kcp_id = the new connection ID, that is, configure the UDP SockFd of the new protocol specified context structure of the client. Further, send a connection request packet and determine whether the connection is successful. If successful, return the operation success. If failed, return the operation code and handle the error.
[0081] Here, the peer_addr field in the UDP SockFd is filled with the network address and listening port of the server.
[0082] In some optional ways, the server is further configured to, in response to determining that the target specified context structure is the initial protocol specified context structure, control network IO operations according to the corresponding operation logic in the initial protocol.
[0083] In this implementation method, if the target specified context structure is the initial protocol specified context structure, the control right of the network IO operation is returned to the corresponding operation logic in the initial protocol, that is, control the network IO operation according to the corresponding operation logic in the initial protocol, so that the application program still supports the original communication protocol, that is, the initial protocol, and can communicate with clients that support the initial protocol.
[0084] In some alternative ways, a global lock-free hash table is configured to store the listening specified context structure, the new protocol specified context structure, and the initial protocol specified context structure.
[0085] In this implementation, the server can configure a global lock-free hash table to store the listening specified context structure, the new protocol specified context structure, and the initial protocol specified context structure.
[0086] Here, the listening specified context structure, the new protocol specified context structure, and the initial protocol specified context structure are respectively used to maintain different socket descriptors. The global lock-free hash table can use the socket descriptor as the key value to index the specified context structure.
[0087] Among them, the lock-free hash table is a multi-threaded safe hash table, and most of them are implemented by atomic operations such as CAS. Since there is no need to lock during concurrent access, its concurrent performance is better than the implementation of an ordinary hash table + lock.
[0088] Here, CAS is a hardware synchronization primitive provided by a processor (CPU) that supports concurrency. The CAS operation contains three operands, namely the memory location (V), the expected original value (A), and the new value (B), written as CAS(V, A, B). If the value of the memory location matches the expected original value, then the processor will automatically update the value of that location to the new value. Otherwise, the processor does nothing. In either case, it returns the value of that location before the CAS instruction. Atomic operations implemented based on the CAS mechanism often use the CAS loop method: that is, if the CAS operation fails (that is, the expected original value A and the value stored in the memory location V are not equal), it will repeatedly loop and try to perform the CAS operation until the operation is successful.
[0089] This implementation stores the listening specified context structure, the new protocol specified context structure, and the initial protocol specified context structure by configuring a global lock-free hash table, which speeds up the search speed of the specified context structure.
[0090] Continue to refer to Figure 2 , Figure 2 is an architecture diagram of the application scenario of the system implemented according to the data communication protocol of this embodiment.
[0091] In Figure 2In the application scenario, the initial protocol is the TCP / IP protocol, and the new protocol is the KCP protocol (i.e., a kind of RUDP protocol) with UDP protocol as the network transport layer. The server injects the target dynamic library into the second application to take over the network IO-related library functions, such as socket, bind, listen, accept, send, recv and other library functions. For the KCP client, the target dynamic library is injected into the first application to take over the network IO-related library functions, such as socket, connect, send, recv and other library functions. Based on the callback interface of the network IO-related library function hooks, the server replaces the initial protocol of the second application with the new protocol. Further, the server can pre-configure and monitor a specified context structure, i.e., Listen SockFd, based on the callback interface of the network IO-related library function hooks. The specified context structure is used to maintain the socket descriptor. For the monitored specified context structure, in response to determining that there is a monitored socket indicating that a new connection request has arrived, a target specified context structure corresponding to the monitored socket is created, and the address and port of the peer are filled in the first address field in the target specified context structure. In response to determining that the target specified context structure is the new protocol specified context structure, i.e., TCP SockFd, and the send or receive state is ready, data transmission is performed with the client based on the address and port of the peer in the first address field.
[0092] Among them, the KCP protocol is an open-source protocol of the transport layer ARQ (Automatic Repeat reQuest) with reliability. Its design is to solve the problem of slow transmission speed of the TCP protocol in the case of network congestion. KCP strives to improve the transmission speed on the premise of ensuring reliability. Its focus is mainly on controlling the reliability of data and improving the transmission speed. Therefore, it does not specify its underlying transmission protocol (most implementations based on KCP use UDP as the underlying transmission protocol). The data packet of the KCP layer protocol adds a control header on the basis of the data packet of the underlying transmission protocol. Since the transmission delay of data in the network is not fixed, it is difficult to predict the timeout retransmission time. To judge the loss of data packets as early as possible, KCP introduces a fast retransmission mechanism. Compared with the TCP protocol, the fast retransmission mechanism of KCP improves the transmission performance by 20% in a weak network (severe delay jitter or high packet loss rate) environment at the cost of occupying about 20% more network bandwidth and CPU occupancy rate.
[0093] RUDP, that is, the Reliable User Datagram Protocol, is a simple packet transmission protocol based on the Reliable Data Protocol (RDP: RFC908 and 1151 (Second Edition)). As a reliable transport protocol, RUDP is used to transmit telephone signals between IP networks. It allows each connection attribute to be configured independently, so that the protocol can be implemented under different transmission requirements on different platforms.
[0094] Figure 3 Fig. 300 shows the flow of an embodiment of a data communication protocol implementation method that can be applied to this application. In this embodiment, the data communication protocol implementation method includes the following steps.
[0095] Step 301, injecting a target dynamic library into a target application to take over network IO-related library functions.
[0096] In this embodiment, the execution entity can inject a target dynamic library into a target application to take over network IO-related library functions. For example, for connection-oriented protocols, the IO-related library functions can be socket, bind, listen, accept, recv, send, close. For connectionless protocols, the related library functions can be recv and send, sendmsg and recvmsg, etc., that is, the dynamic library injection method is used to take over network IO-related library functions.
[0097] Step 302, replacing the initial protocol of the target application with a new protocol based on network IO-related library function hooks.
[0098] In this embodiment, the execution entity can replace the initial protocol of the target application with a new protocol according to network IO-related library function hooks or the callback interface of network IO-related library functions, that is, callback.
[0099] Here, the new protocol and the initial protocol can both be connection-oriented protocols, such as the TCP protocol, etc. The new protocol and the initial protocol can both be connectionless protocols, such as the UDP protocol, etc. The new protocol and the initial protocol can both be data stream protocols or datagram protocols. The new protocol and the initial protocol can also both be reliable protocols or unreliable protocols. This application does not make any restrictions on this.
[0100] In some optional ways, the method further includes: simultaneously listening for connection establishment requests of the original protocol and the new protocol, and performing data transmission based on the new protocol or the initial protocol.
[0101] In this implementation, the new protocol is a communication protocol for a client / server architecture. The execution entity can be configured to listen for a specified context structure. For listening for the specified context structure, in response to determining that there is a listening socket indicating that a new connection request has arrived, a target specified context structure corresponding to the listening socket is created, and the address and port of the peer are filled in the first address field in the target specified context structure. Among them, the specified context structure for listening includes a new protocol listening socket, an initial protocol listening socket, and a second address. The second address is used to store the bound address and the listening port. The target specified context structure is a new protocol specified context structure or an initial protocol specified context structure. In response to determining that the target specified context structure is a new protocol specified context structure and the sending or receiving state is ready, data transmission is performed with the client based on the address and port of the peer in the first address field.
[0102] For further reference Figure 4 , as an implementation of the methods shown in the above figures, the present application provides an embodiment of a data communication protocol implementation device. This device embodiment corresponds to Figure 3 the method embodiment shown, and this device can be specifically applied to various electronic devices.
[0103] As Figure 4 shown, the data communication protocol implementation device 400 in this embodiment includes: an injection module 401 and a replacement module 402.
[0104] Among them, the injection module 401 can be configured to inject a target dynamic library into a target application program to take over network IO-related library functions.
[0105] The replacement module 402 can be configured to replace the initial protocol of the target application program with a new protocol based on network IO-related library function hooks.
[0106] In some optional ways of this embodiment, the device further includes a listening module, which is configured to simultaneously listen for connection establishment requests of the original protocol and the new protocol, and perform data transmission based on the new protocol or the initial protocol.
[0107] According to the embodiments of the present application, the present application also provides an electronic device and a readable storage medium.
[0108] As Figure 5 shown, it is a block diagram of an electronic device for the data communication protocol implementation method according to the embodiments of the present application.
[0109] FIG. 500 is a block diagram of an electronic device implementing a data communication protocol method according to an embodiment of the present application. The electronic device is intended to represent various forms of digital computers, such as, a laptop computer, a desktop computer, a workbench, a personal digital assistant, a server, a blade server, a mainframe computer, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as, a personal digital processor, a cellular phone, a smart phone, a wearable device, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present application described and / or claimed herein.
[0110] As Figure 5 shown, the electronic device includes: one or more processors 501, a memory 502, and interfaces for connecting the various components, including a high-speed interface and a low-speed interface. The various components are interconnected using different buses and may be mounted on a common motherboard or otherwise mounted as required. The processor may process instructions executed within the electronic device, including instructions stored in the memory or on the memory to display graphical information of a GUI on an external input / output device (such as, a display device coupled to the interface). In other embodiments, multiple processors and / or multiple buses may be used in conjunction with multiple memories and multiple memories if desired. Similarly, multiple electronic devices may be connected, each device providing a portion of the necessary operations (such as, as a server array, a set of blade servers, or a multi-processor system). Figure 5 In
[0111] FIG., a single processor 501 is taken as an example.
[0112] The memory 502 is the non-transitory computer-readable storage medium provided by the present application. Among them, the memory stores instructions executable by at least one processor, so that the at least one processor executes the data communication protocol implementation method provided by the present application. The non-transitory computer-readable storage medium of the present application stores computer instructions for causing a computer to execute the data communication protocol implementation method provided by the present application. Figure 4 The memory 502, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs, non-transitory computer-executable programs, and modules, such as program instructions / modules corresponding to the data communication protocol implementation method in the embodiments of the present application (for example,
[0113] The memory 502 may include a program storage area and a data storage area. The program storage area may store an operating system and application programs required for at least one function. The data storage area may store data created for use of the electronic device implementing the data communication protocol, etc. In addition, the memory 502 may include high-speed random access memory, and may also include non-transitory memory, such as at least one magnetic disk storage device, a flash memory device, or other non-transitory solid-state storage devices. In some embodiments, the memory 502 may optionally include a memory remotely disposed relative to the processor 501, and these remote memories may be connected to the electronic device implementing the data communication protocol through a network. Examples of the above network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0114] The electronic device implementing the data communication protocol method may further include: an input device 503 and an output device 504. The processor 501, the memory 502, the input device 503, and the output device 504 may be connected through a bus or other means. Figure 5 Take connection through the bus as an example.
[0115] The input device 503 may receive input digital or character information, such as input devices like a touch screen, a keypad, a mouse, a trackpad, a touchpad, a pointing stick, one or more mouse buttons, a trackball, a joystick, etc. The output device 504 may include a display device, an auxiliary lighting device (e.g., an LED), and a haptic feedback device (e.g., a vibration motor), etc. The display device may include but is not limited to a liquid crystal display (LCD), a light-emitting diode (LED) display, and a plasma display. In some embodiments, the display device may be a touch screen.
[0116] Various embodiments of the systems and techniques described herein may be implemented in digital electronic circuit systems, integrated circuit systems, dedicated ASICs (application specific integrated circuits), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include: implemented in one or more computer programs, the one or more computer programs may be executed and / or interpreted on a programmable system including at least one programmable processor, the programmable processor may be a dedicated or general-purpose programmable processor, and may receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit the data and instructions to the storage system, the at least one input device, and the at least one output device.
[0117] These computing procedures (also known as programs, software, software applications, or code) include machine instructions for a programmable processor and can implement these computing procedures using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. As used herein, the terms "machine-readable medium" and "computer-readable medium" refer to any computer program product, device, and / or apparatus (e.g., a magnetic disk, an optical disk, a memory, a programmable logic device (PLD)) for providing machine instructions and / or data to a programmable processor, including a machine-readable medium that receives machine instructions as a machine-readable signal. The term "machine-readable signal" refers to any signal for providing machine instructions and / or data to a programmable processor.
[0118] In order to provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and a pointing device (e.g., a mouse or a trackball) through which the user can provide input to the computer. Other kinds of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0119] The systems and techniques described herein can be implemented in a computing system including backend components (e.g., as a data server), or a computing system including middleware components (e.g., an application server), or a computing system including frontend components (e.g., a user computer having a graphical user interface or a web browser through which the user can interact with an implementation of the systems and techniques described herein), or a computing system including any combination of such backend components, middleware components, or frontend components. The components of the system can be interconnected to each other by digital data communication in any form or medium (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.
[0120] A computer system can include a client and a server. The client and the server are generally remote from each other and typically interact through a communication network. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other.
[0121] According to the technical solution of the embodiment of the present application, a transparent replacement of the application program communication protocol is realized.
[0122] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps recited in this application can be executed in parallel, sequentially, or in different orders, as long as the desired results of the technical solutions disclosed in this application can be achieved, and no limitations are imposed herein.
[0123] The above specific embodiments do not constitute a limitation on the scope of protection of this application. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this application shall be included within the scope of protection of this application.
Claims
1. A data communication protocol implementation system, the system comprises: a first communication entity and a second communication entity that communicate with each other based on an initial protocol; the first communication entity is configured to inject a first dynamic library into a first application to take over library functions related to network IO; based on library function hooks related to network IO, replace the initial protocol of the first application with a new protocol; the second communication entity is configured to inject a second dynamic library into a second application to take over library functions related to network IO; based on library function hooks related to network IO, replace the initial protocol of the second application with a new protocol; wherein, the first dynamic library is the same as the second dynamic library.
2. The system according to claim 1, wherein the new protocol is a communication protocol of a client / server architecture, the first communication entity is a client, and the second communication entity is a server; the first communication entity is configured to execute library functions of the new protocol of the client when initiating a connection; the second communication entity is configured to execute library functions of the new protocol of the server when listening for connection establishment.
3. The system according to claim 2, wherein, the server is configured to simultaneously listen for connection establishment requests of the initial protocol and the new protocol, and perform data transmission based on the new protocol; or, the server is configured to simultaneously listen for connection establishment requests of the initial protocol and the new protocol, and perform data transmission based on the initial protocol.
4. The system according to claim 3, for simultaneously listening for connection establishment requests of the initial protocol and the new protocol, and performing data transmission based on the new protocol, comprises: configuring a listening specified context structure, for the listening specified context structure, in response to determining that there is a listening socket indicating that a new connection request has arrived, creating a target specified context structure corresponding to the listening socket, and filling the address and port of the peer into the first address field in the target specified context structure, wherein the listening specified context structure includes a new protocol listening socket, an initial protocol listening socket, and a second address, the second address is used to store the bound address and listening port, and the target specified context structure is a new protocol specified context structure or an initial protocol specified context structure; in response to determining that the target specified context structure is a new protocol specified context structure and the send or receive status is ready, perform data transmission with the client based on the address and port of the peer in the first address field.
5. The system according to claim 3, for simultaneously listening for connection establishment requests of the initial protocol and the new protocol, and performing data transmission based on the initial protocol, comprises: Configure a listening specified context structure. For the listening specified context structure, in response to determining that there is a listening socket indicating that a new connection request has arrived, create a target specified context structure corresponding to the listening socket, and fill the address and port of the peer into the first address field in the target specified context structure. Among them, the listening specified context structure includes a new protocol listening socket, an initial protocol listening socket, and a second address, and the second address is used to store the bound address and the listening port. The target specified context structure is a new protocol specified context structure or an initial protocol specified context structure; In response to determining that the target specified context structure is an initial protocol specified context structure, control the network IO operation according to the corresponding operation logic in the initial protocol to perform data transmission with the client.
6. The system according to claim 4, wherein, the server is further configured to: Configure a callback interface for library function calls to monitor whether the listening specified context structure indicates that a new connection request has arrived, whether the reception status of the new protocol specified context structure is ready, and whether the transmission status is ready.
7. The system according to claim 4, the server is further configured to: Configure a global lock-free hash table to store the listening specified context structure, the new protocol specified context structure, and the initial protocol specified context structure.
8. A method for implementing a data communication protocol, applied to a second communication entity, where the second communication entity communicates with a first communication entity based on an initial protocol, and the method includes: Inject a target dynamic library into a target application program to take over the network IO related library functions. The target dynamic library includes a second dynamic library, and the target application program includes a second application program; Based on the network IO related library function hooks, replace the initial protocol of the target application program with a new protocol. The first communication entity is used to inject a first dynamic library into a first application program to take over the network IO related library functions; Based on the network IO related library function hooks, replace the initial protocol of the first application program with a new protocol. The first dynamic library is the same as the second dynamic library.
9. The method according to claim 8, wherein, the new protocol is a communication protocol of a client / server architecture, and the method further includes: Simultaneously monitor the connection establishment requests of the initial protocol and the new protocol, and perform data transmission based on the new protocol or the initial protocol, specifically including: Configure a listening specified context structure. For the listening specified context structure, in response to determining that there is a listening socket indicating that a new connection request has arrived, create a target specified context structure corresponding to the listening socket, and fill the address and port of the peer in the first address field in the target specified context structure. Wherein, the listening specified context structure includes a new protocol listening socket, an initial protocol listening socket, and a second address, and the second address is used to store the bound address and listening port. The target specified context structure is a new protocol specified context structure or an initial protocol specified context structure; In response to determining that the target specified context structure is a new protocol specified context structure and the send or receive status is ready, perform data transmission with the client based on the address and port of the peer in the first address field.
10. A data communication protocol implementation device, applied to a second communication entity, where the second communication entity communicates with a first communication entity based on an initial protocol, and the device includes: An injection module, configured to inject a target dynamic library into a target application program to take over network IO related library functions. The target dynamic library includes a second dynamic library, and the target application program includes a second application program; A replacement module, configured to replace the initial protocol of the target application program with a new protocol based on network IO related library function hooks. The first communication entity is used to inject a first dynamic library into a first application program to take over network IO related library functions; Based on network IO related library function hooks, replace the initial protocol of the first application program with a new protocol. The first dynamic library is the same as the second dynamic library.
11. An electronic device, characterized in that, it includes: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, enabling the at least one processor to execute the method according to any one of claims 8-9.
12. A non-transitory computer-readable storage medium storing computer instructions, characterized in that, The computer instructions are used to cause the computer to execute the method according to any one of claims 8-9.
Citation Information
Patent Citations
Method for realizing a firewall of a database
CN109657491A