Network communication protocol code generation method based on model refinement

By constructing UML diagrams and finite state automata to describe the process model, and combining the CommLib and CryptoLib libraries to generate network communication protocol code, the problem of inaccurate understanding when developers manually implement the protocol is solved, the accuracy and consistency of code generation are achieved, and development efficiency is improved.

CN119109992BActive Publication Date: 2025-10-10INST OF SOFTWARE - CHINESE ACAD OF SCI
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310673115.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-06-07
Publication Date
2025-10-10
Estimated Expiration
2043-06-07

AI Technical Summary

Technical Problem

In the design and implementation of network communication protocols, developers often have inaccurate understanding when manually implementing the protocols, resulting in implementation errors that are difficult to detect. Manual simulation testing cannot guarantee code consistency, and existing technologies lack effective solutions.

Method used

By constructing UML diagrams and finite state automata to describe the process model, combined with CommLib and CryptoLib libraries, network communication protocol code is generated. Model refinement technology and encryption and decryption algorithm refinement technology are used to ensure the accuracy and consistency of code generation.

Benefits of technology

It improves the accuracy and development efficiency of network communication protocol code generation, ensures that the generated code is consistent with the model, reduces potential errors in manual implementation, and provides an automated protocol code framework.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119109992B_ABST
    Figure CN119109992B_ABST
Patent Text Reader

Abstract

The application discloses a network communication protocol code generation method based on model refinement, relates to the field of code generation, and generates and refines codes through a built model, a network communication protocol library and an encryption protocol library, wherein communication between processes of network nodes in the network communication protocol is refined through a communication method refinement technology, and encryption and decryption of communication data are refined through an encryption and decryption algorithm refinement technology, so that the accuracy of generated codes is improved, and development efficiency is further improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of code generation, and in particular to a network communication protocol code generation method based on model refinement. Background Art

[0002] With the rapid development of the internet, computers are communicating constantly, and the confidentiality of computer information is becoming increasingly important. Data security transformation, or cryptography, is the most practical and reliable method for protecting computer information. Cryptography is the primary means of achieving secret communication and is a specialized symbol used to conceal language, text, and images. Any communication method that uses specialized symbols to obscure the original form of a message, according to a method agreed upon by both parties, making it unreadable to third parties is called cryptographic communication. In computer communications, cryptographic technology is used to conceal information before it is transmitted. This ensures that even if the information is stolen or intercepted during transmission, the thief cannot understand the content, thus ensuring the security of information transmission.

[0003] In the design and implementation of network communication protocols, developers typically manually implement the protocol after the protocol model is designed. This introduces potential security risks. Due to the possibility of inaccurate understanding of the protocol by the developer, the semantics of the protocol implementation may be inconsistent with the semantics defined in the model, leading to implementation errors. Furthermore, directly deploying the protocol on network nodes makes such errors difficult to detect. However, manually implementing the protocol locally for simulation testing increases the developer's workload and still cannot fully guarantee the consistency between the deployed manual code and the simulated manual implementation code. Known communication protocols and encryption and decryption algorithms are all hand-coded, making them prone to errors. Currently, there is no effective solution to this problem. Summary of the Invention

[0004] The purpose of the present invention is to address the deficiencies of the existing technology and propose a network communication protocol code generation method based on model refinement. Code generation and refinement are performed through the constructed model and network communication protocol library and encryption protocol library. The communication between processes of network nodes in the network communication protocol is refined through the communication method refinement technology, and the encryption and decryption of communication data is refined through the encryption and decryption algorithm refinement technology, thereby improving the accuracy of the generated code and thus improving development efficiency.

[0005] To achieve the above object, the present invention adopts the following technical solutions:

[0006] A network communication protocol code generation method based on model refinement includes the following steps:

[0007] 1) Pre-build a model that describes the class of each process to be implemented, its member variables, and member methods using UML diagrams, and specifies the types of member methods. It also describes the possible states of each process and the transitions between states using finite state automata.

[0008] 2) Pre-build the network communication protocol library CommLib and the encryption protocol library CryptoLib. CommLib implements simulated and real communication between processes based on the network communication interface functions of the data link layer and the transport layer packet transmission; CryptoLib implements encryption and decryption of communication data between processes based on the interface functions of symmetric encryption, asymmetric encryption and data signing.

[0009] 3) When generating code, read in the pre-built model and use it to obtain the data structures required for code generation, including the process data structure Process and the user-defined data structure type UserType;

[0010] 4) Generate code for each UserType and generate a user-defined type header file UserType.hpp based on the generated code string;

[0011] 5) For each Process, use the model to generate the corresponding header file, and use CommLib, CryptoLib and the model to generate the corresponding source file;

[0012] 6) Generate a python script file in the file system according to the file names of the UserType.hpp file, header file and source file. The script compiles the source file and then compiles each process to generate a binary executable file.

[0013] Furthermore, the finite state automaton in step 1) includes a state set and an edge set, each state is identified by a unique ID, and each edge is represented by a triple to represent the starting state, the ending state, and the code to be executed when transitioning between states.

[0014] Furthermore, the analog communication in step 2) includes a synchronous channel and / or an asynchronous channel;

[0015] In real-world communication, use the libpcap library's monitoring method to monitor custom Ethernet frames sent to implement Ethernet communication within the local area network; use the AF_INET address family and SOCK_DGRAM to indicate the sending of unsecured network datagrams to implement UDP communication; and / or

[0016] The encryption algorithms included in CryptoLib include AES, RSA and HASH.

[0017] Furthermore, the step of generating UserType.hpp in step 4) includes:

[0018] Use an empty string to store the currently generated code string;

[0019] Generate code for each UserType, using the name of UserType as the class name to generate a definition string for the class. The member data and method definitions of the class are generated in sequence according to the definitions in UserType, and the generated string is concatenated with the generated code string.

[0020] Create UserType.hpp in the file system and input the concatenated code string into UserType.hpp.

[0021] Furthermore, the header file generated in step 5) is a C++ header file, wherein each header file corresponds to a defined process in a network communication node, and the header file only contains a definition of a class.

[0022] Furthermore, in step 5), the header file describes the behavior logic of the communication node process in the main function, defines the public key and private key information of the current process and the public key information of other communication node processes, and implements the state migration and behavior logic of the current process through a finite state machine.

[0023] Furthermore, the step of generating a header file in step 5) includes:

[0024] 1) Create an empty string as the generated string;

[0025] 2) Generate the header file that the process depends on and concatenate the generated string to the existing string;

[0026] 3) Generate the data structure definition of the process, including class member variables, member methods and declarations, and concatenate them to the generated string;

[0027] 4) Generate code for the finite state machine corresponding to the behavior of the process based on the model, and concatenate the generated code to the generated string;

[0028] 5) Create a header file with the name of the process in the file system and write the generated string into the header file.

[0029] Furthermore, a method for generating code for a finite state machine corresponding to the behavior of the process according to the model is as follows: using integer variables to simulate states, using while loop structures and switch-case branches to simulate migration behaviors of different states, and thereby generating code.

[0030] Furthermore, the source files generated in step 5) are C++ source files, each of which corresponds to a header file with the same name, and the source files contain the communication methods and data encryption methods between processes containing communication operations implemented by interfaces and functions provided by CommLib and CryptoLib.

[0031] Furthermore, the step of generating the source file in step 5) includes:

[0032] 1) Create an empty string as the generated string;

[0033] 2) Based on the definition of the non-encryption and non-communication method of the process, a template string for implementing the common method is given and the string is concatenated to the generated string;

[0034] 3) For each encryption / decryption method in the process, obtain the encryption / decryption algorithm used by it through the model, and generate a code that calls the relevant interface in the pre-implemented data encryption library. This code, as the implementation of the encryption / decryption method, will be spliced ​​after the generated string;

[0035] 4) For each method in the process that receives or sends network data packets, the model obtains the type of network protocol used, and generates a code that calls the relevant interface in the pre-implemented network communication library. This code, as the implementation of the method for receiving / sending messages, will be concatenated to the generated string;

[0036] 5) Create a source file in the file system with the name of the process and write the generated string into the source file.

[0037] Compared with the prior art, the present invention has the following beneficial effects:

[0038] This invention provides a model-refined C++ code generation method for network communication protocols on the Linux platform. This method can be gradually refined into compilable code based on the model. Developers can use this patented method to refine the model and ultimately generate code, improving development efficiency. The tool provides developers with an automated protocol code framework implementation by providing two solutions: simulated execution code generation and real-world code generation. Model consistency ensures behavioral consistency between the generated simulated code and the real-world code, resolving the aforementioned implementation issues. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figure 1 Generate a flow chart for the network communication protocol code based on model refinement of the present invention;

[0040] Figure 2 Schematic diagram of code refinement for communication method refinement technology and encryption and decryption algorithm refinement technology.

[0041] Figure 3 It is a schematic diagram of simulated communication;

[0042] Figure 4 This is the AES encryption and decryption flow chart;

[0043] Figure 5 This is the RSA encryption and decryption flow chart. DETAILED DESCRIPTION

[0044] In order to make the above features and advantages of the present invention more clearly understood, embodiments are given below and described in detail with reference to the accompanying drawings.

[0045] This embodiment proposes a network communication protocol code generation method based on model refinement, such as Figure 1 The specific processing steps are as follows:

[0046] Step 1: Pre-build a formal model. This model is a data structure that includes a UML diagram describing the data structure of each process to be implemented. The UML diagram clearly describes the class that implements each process, as well as the member variables and member methods contained in the class. The model also specifies whether the member method type is an encryption method, a communication method, or a normal method. The model also includes a finite state automaton corresponding to each process, which is used to describe the possible states of each process in the system and the transitions between states. The finite state automaton contains a set of states and a set of edges. Each state can be identified by a unique ID (integer). Each edge is represented by a triple (s1, c, s2), where s1 and s2 represent the starting state and the ending state, and c is the code executed from the starting state s1 to the ending state s2. Code c may call member methods and member variables defined in the UML.

[0047] Step 2: Pre-build the network communication protocol library CommLib and the encryption protocol library CryptoLib, and implement the communication method refinement technology and encryption and decryption algorithm refinement technology based on these two libraries. The communication method refinement technology is used to refine the communication between the processes of the network nodes in the network communication protocol, and the encryption and decryption algorithm refinement technology is used to refine the encryption and decryption of the communication data, such as Figure 2 shown.

[0048] 1) The communication protocol library CommLib implements network communication interface functions based on the transport layer interface provided by the C++ language and the Ethernet frame monitoring interface provided by the network packet capture function library pcaplib, which can be based on the data link layer and transport layer packet transmission, and realizes the essential communication method technologies described above.

[0049] The communication method refinement technology mainly includes simulated communication and real communication:

[0050] The main modes of analog communication are synchronous channels and asynchronous channels (such as Figure 3 As shown in Figure 2, an asynchronous channel stores a list of messages and uses names to identify the communication nodes. Network nodes using channels can communicate by joining and retrieving messages from the channel. Synchronous channels allow for synchronous communication. Two communicating processes can only communicate if they simultaneously execute the member methods for sending and receiving messages. Otherwise, they will be blocked waiting for the other party to send or receive messages.

[0051] Real-world communication utilizes existing software libraries related to Ethernet communication and socket programming. The libpcap library (network packet capture library) provides methods for accessing the network card's send and listen interfaces. In the real-world communication method, the libpcap library's send method is used to broadcast custom Ethernet frames to the local area network. The libpcap library also implements monitoring and filtering for netfilter (a Linux subsystem for managing hook functions). Therefore, the libpcap library's listen method monitors the sent custom Ethernet frames, enabling Ethernet communication within the local area network. For example, using the UDP protocol in real-world communication, using the AF_INET address family and SOCK_DGRAM when creating a socket to indicate unsecured network datagrams enables UDP communication. During code generation, if you generate code for the send / receive methods in a process that uses UDP communication, method implementations containing the sendto and recvfrom functions will be generated for synchronous sending and receiving of datagrams.

[0052] The process of automatically generating the communication framework of the communication protocol in the subsequent code generation step needs to utilize the currently known real communication library and the implemented simulation communication library to provide technical solutions for the simulated communication and real communication that may be used when automatically generating the communication protocol. This method has two application scenarios. One is to generate code locally to verify that the designed network protocol meets expectations. At this time, simulated communication, that is, inter-process communication, is needed to simulate the communication between network nodes in the actual environment; the other is to generate binary files that can be directly deployed to network nodes. At this time, real communication is used. Select one of the communication methods for generation in the code generation. Among them, simulated communication mainly includes synchronous channels and asynchronous channels. Synchronous and asynchronous communications can exist simultaneously or separately in the generated code. The sending method of the synchronous channel must wait for acceptance before executing the subsequent statements. There is no such requirement for the asynchronous channel. Real communication requires the use of Ethernet broadcast communication and monitoring as well as sending and receiving functions based on the UDP protocol.

[0053] 2) The encryption protocol library CryptoLib is based on the encryption communication library openssl, and provides interface functions for symmetric encryption, asymmetric encryption and data signing, that is, it implements the encryption and decryption algorithm code refinement technology.

[0054] The encryption and decryption algorithm code refinement technology mainly uses three encryption algorithms: AES, RSA and HASH:

[0055] like Figure 4 As shown, using AES encryption and decryption first requires breaking the data to be transmitted into multiple plaintext blocks, which are then encrypted using an AES cipher. Similarly, AES decryption at the receiving end of process communication uses the encryption key to perform the inverse operation, recovering the plaintext from the ciphertext block. AES ciphers have different encryption modes, such as CBC and ECB. ECB mode directly uses the cipher to perform bitwise operations on the original data to generate ciphertext blocks. CBC mode encrypts plaintext blocks sequentially and uses the encrypted result to encrypt the next plaintext block. CBC encryption is more secure than ECB but is not conducive to parallel computing.

[0056] like Figure 5 As shown, RSA encryption uses a public-private key pair. The public key encrypts, the private key decrypts, and the public key is publicly known. The simplest RSA encryption algorithm involves the following elements:

[0057] N: large integer N, called modulus;

[0058] p and q: two factors of the large integer N;

[0059] e and d: two exponents that are modular inverses of each other;

[0060] c and m: ciphertext and plaintext respectively. Here, it generally refers to a decimal number. Another one is the Euler function value of n, which is often used when solving d.

[0061] The RSA key generation process is as follows: First, select two large prime numbers p and q that meet the requirements, calculate n = p * q, and calculate the Euler function value of n φ(n) = (p-1) * (q-1). Then, select an integer e∈(1,φ(n)) with gcd(φ(n),e) = 1, and calculate d using d*e≡1modφ(n). Then, {e,n} is the public key, and {d,n} is the secret key.

[0062] Common hash function encryption algorithms include MD5 and the SHA series. A hash encryption algorithm is a one-way, irreversible algorithm that compresses a message of any length by iterating through a hash function to generate a message digest.

[0063] In the code generated by the present invention, the relevant encryption and decryption interfaces of CryptoLib will be specifically called to perform the encryption and decryption operations implemented by the generated protocol. For example, the RSA encryption method will read in the char array and public key to be encrypted, and the RSA decryption method will read in the ciphertext and private key for decryption.

[0064] Code generation is performed based on the constructed model and library. The primary function of code generation is to read the data structures in the model and, using the environment provided by the aforementioned network communication protocol library, CommLib, and cryptographic protocol library, CryptoLib, to generate and implement the protocols described in the model. Through the code generation process of the present invention, an implementation of the network protocol is created in the file system. The specific steps for code generation are as follows.

[0065] Step 3: Read in the pre-built model and use it to obtain the data structure required to generate the code, namely the process data structure (Process) given in the model and the predefined or customized data structure type (UserType) used by the user.

[0066] Step 4: Using the obtained UserType data structure type, automatically generate the user-defined type header file UserType.hpp. The generated UserType.hpp contains all data types used in the model but have no corresponding corresponding data types in the C++ language. The generation process is:

[0067] Use an empty string to store the currently generated code;

[0068] Code generation is performed for each custom data structure type (UserType) in the model. That is, a class definition string is generated with the name of the UserType as the class name. The member data and method definitions of the class are generated in sequence according to the definitions in the UserType data structure.

[0069] The generated string will be concatenated with the generated code string, and a UserType.hpp file will be created in the file system. The final generated code string will be output to the UserType.hpp file.

[0070] Step 5: Generate the header and source files described in the output using the obtained Process collection. Specifically, for each process in the Process collection, generate its corresponding header and source files.

[0071] This header file is specifically a C++ header file, each of which corresponds to a process in a network communication node defined in the model. This header file contains only one class definition, which is consistent with the definition of the communication node process with the same name described in the model. Furthermore, the header file describes the behavior logic of the communication node process (this process) in the main function. This section defines the public and private key information of this process, as well as the public key information of other communication node processes. It also implements the different behaviors of this process in different states through a finite state machine, thereby implementing the state transitions and behavioral logic of this process.

[0072] The generation of this header file consists of five steps:

[0073] 1) Create an empty string as the generated string.

[0074] 2) Generate the header file that Process depends on and concatenate the generated string to the existing string.

[0075] 3) Generate the data structure definition of the process, that is, the definition of the class member variables and member methods in the process in the model, correctly generate their declarations according to their names and types, and splice them into the generated string.

[0076] 4) Generate code based on the finite state machine corresponding to the process behavior in the model. The specific approach is to use integer variables to simulate the state of the finite state machine, use while loop structures and switch-case branches to simulate the migration behavior of different states, and splice the generated code to the generated string.

[0077] 5) Create a C++ header file named after the process in the header folder in the file system and write the generated string to the file.

[0078] The source file is specifically a C++ source file, each of which corresponds to a header file in a header folder with the same name. The source file includes a communication method for process members containing communication operations, a process data encryption method containing data encryption, and implementations of other methods implemented using interfaces and functions provided by two pre-built libraries.

[0079] The generation of this source file consists of five steps:

[0080] 1) Create an empty string as the generated string.

[0081] 2) Generation of non-encryption and non-communication methods of the process, that is, according to the definition of non-encryption and non-communication methods of the process, a template string for implementing the common method is given, and the string is concatenated with the generated string.

[0082] 3) Generation and refinement of process encryption / decryption methods, that is, for each encryption / decryption method in the process, the encryption / decryption algorithm used is obtained through the model, and a code that calls the relevant interface in the pre-implemented data encryption library is generated. This code, as the implementation of the encryption / decryption method, will be spliced ​​after the generated string.

[0083] 4) Generation and refinement of process receiving / sending methods, that is, for each method that receives or sends network data packets in the process, the network protocol type used is obtained through the model, and a code that calls the relevant interface in the pre-implemented network communication library is generated. This code, as the implementation of the method for receiving / sending messages, will be spliced ​​after the generated string.

[0084] 5) Create a C++ source file with the current process name in the source file folder of the file system and write the generated string to the file.

[0085] Step 6: Based on the file names of the generated UserType.hpp file, header file, and source file, a Python script file compiled using the g++ compiler is generated in the file system. The script compiles the generated source file to produce a .o file, which is then linked. A binary executable file is compiled for each process, which can be used for deployment and communication of the corresponding communication node of the process.

[0086] Experimental test:

[0087] To verify the feasibility of the proposed method, this experiment modeled and generated code for both the Needham and 802.1x protocols. The experiments demonstrated that the proposed code generation method was able to quickly and accurately generate corresponding protocol implementations in both instances. After compiling the generated code, it could be deployed and run on network nodes.

[0088] Although the present invention has been disclosed as above by way of embodiments, they are not intended to limit the present invention. Any appropriate modification or equivalent substitution of the technical solution of the present invention by a person skilled in the art should be included in the protection scope of the present invention. The protection scope of the present invention shall be based on that defined in the claims.

Claims

1. A network communication protocol code generation method based on model refinement, characterized in that: The following steps are involved: 1) Pre-build a model that describes the class of each process to be implemented, its member variables, and member methods using UML diagrams, and specifies the types of member methods. It also describes the possible states of each process and the transitions between states using finite state automata. 2) Pre-build the network communication protocol library CommLib and the encryption protocol library CryptoLib. CommLib implements simulated and real communication between processes based on the network communication interface functions of the data link layer and the transport layer packet transmission; CryptoLib implements encryption and decryption of communication data between processes based on the interface functions of symmetric encryption, asymmetric encryption and data signing. 3) When generating code, read in the pre-built model and use it to obtain the data structures required for code generation, including the process data structure Process and the user-defined data structure type UserType; 4) Generate code for each UserType and generate a user-defined type header file UserType.hpp based on the generated code string; 5) For each Process, use the model to generate the corresponding header file, and use CommLib, CryptoLib and the model to generate the corresponding source file; 6) Generate a python script file in the file system according to the file names of the UserType.hpp file, header file and source file. The script compiles the source file and then compiles each process to generate a binary executable file.

2. The method according to claim 1, wherein In step 1), the finite state automaton contains a state set and an edge set. Each state is identified by a unique ID, and each edge is represented by a triple to represent the starting state, the ending state, and the code to be executed when transitioning between states.

3. The method according to claim 1, wherein In step 2), the analog communication includes a synchronous channel and / or an asynchronous channel; In real-world communication, use the libpcap library's monitoring method to monitor custom Ethernet frames sent to implement Ethernet communication within the local area network; use the AF_INET address family and SOCK_DGRAM to indicate the sending of unsecured network datagrams to implement UDP communication; and / or The encryption algorithms included in CryptoLib include AES, RSA and HASH.

4. The method according to claim 1, wherein The steps to generate UserType.hpp in step 4) include: Use an empty string to store the currently generated code string; Generate code for each UserType, using the name of UserType as the class name to generate a definition string for the class. The member data and method definitions of the class are generated in sequence according to the definitions in UserType, and the generated string is concatenated with the generated code string. Create UserType.hpp in the file system and input the concatenated code string into UserType.hpp.

5. The method according to claim 1, wherein The header file generated in step 5) is a C++ header file, wherein each header file corresponds to a process in a defined network communication node, and the header file only contains the definition of one class.

6. The method according to claim 1 or 5, wherein: In step 5), the header file describes the behavior logic of the communication node process in the main function, defines the public key and private key information of the current process and the public key information of other communication node processes, and implements the state migration and behavior logic of the current process through a finite state machine.

7. The method according to claim 1 or 5, wherein: The steps of generating the header file in step 5) include: Create an empty string as the generated string; Generate the header file that the process depends on, and concatenate the generated string to the existing string; Generate the data structure definition of the process, including class member variables, member methods and declarations, and concatenate them to the generated string; Generate code for the finite state machine corresponding to the behavior of the process based on the model, and concatenate the generated code to the generated string; Create a header file with the name of the process in the file system and write the generated string into the header file.

8. The method according to claim 7, wherein The method for generating code for the finite state machine corresponding to the behavior of the process according to the model is: using integer variables to simulate states, using while loop structures and switch-case branches to simulate the migration behavior of different states, and thus generating code.

9. The method according to claim 1, wherein The source files generated in step 5) are C++ source files, each of which corresponds to a header file with the same name. The source files contain the communication methods and data encryption methods between processes containing communication operations implemented by the interfaces and functions provided by CommLib and CryptoLib.

10. The method according to claim 9, wherein The steps of generating source files in step 5) include: Create an empty string as the generated string; According to the definition of non-encryption and non-communication methods of the process, a template string for implementing the common method is given and the string is concatenated to the generated string; For each encryption / decryption method in the process, the encryption / decryption algorithm used is obtained through the model, and a code is generated to call the relevant interface in the pre-implemented data encryption library. This code, as the implementation of the encryption / decryption method, will be spliced ​​after the generated string; For each method in the process that receives or sends network packets, the network protocol type used is obtained through the model, and a code segment is generated that calls the relevant interface in the pre-implemented network communication library. This code, as the implementation of the method for receiving / sending messages, will be concatenated to the generated string; Create a source file in the file system with the name of the process and write the generated string to the source file.

Citation Information

Patent Citations

  • Network communication program generating method and device

    CN104363285A

  • Restful service development method and device based on SSM framework

    CN112748908A