Network communication protocol code generation method based on model refinement
The method automates network communication protocol code generation using refined models and libraries to ensure accurate and efficient development, addressing implementation errors and security risks.
Patent Information
- Application Number
- CN202310673115.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-07
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2043-06-07
AI Technical Summary
Existing network communication protocol implementations are prone to errors due to developer inaccuracies, leading to security risks and increased development workload, with no effective solution to ensure consistency between simulated and deployed codes.
A network communication protocol code generation method based on model refinement using UML diagrams, finite state automata, and libraries (CommLib, CryptoLib) to automate code generation, ensuring accurate and efficient development.
The method improves development efficiency by generating consistent and secure code through model refinement, addressing implementation errors and security risks.
Smart Images

Figure CN119109992A_ABST
Abstract
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, there is communication between computers all the time, and the confidentiality of computer information is becoming more and more important. Data confidentiality transformation or cryptographic technology is the most practical and reliable method to protect computer information. Passwords are the main means of achieving secret communication and are special symbols that conceal language, text, and images. Any communication method that uses special symbols to conceal the original form of the message in accordance with the method agreed upon by the communicating parties and is not recognized by a third party is called cryptographic communication. In computer communication, cryptographic technology is used to conceal information, and then the concealed information is transmitted, so that even if the information is stolen or captured during the transmission process, the thief cannot understand the content of the information, thereby ensuring the security of information transmission.
[0003] In the design and implementation of network communication protocols, developers usually implement the protocols manually after the protocol model design is completed, so there is a problem that the developer's implementation introduces potential security risks. Due to the possibility that the developer's understanding of the protocol is inaccurate, the implementation semantics of the protocol may be inconsistent with the semantics defined by the model, resulting in implementation errors. In addition, directly deploying the protocol on the network node will make such errors difficult to find, but if the protocol is manually implemented locally for simulation testing, it will increase the workload of developers, and the consistency of the deployed manual code and the simulated manual implementation code cannot be fully guaranteed. Known communication protocols and encryption and decryption algorithms are all manually written and prone to errors. There is currently no effective solution to this problem. Summary of the invention
[0004] The purpose of the present invention is to propose a network communication protocol code generation method based on model refinement in view of the deficiencies in the prior art, wherein code generation and refinement are performed through constructed models and network communication protocol libraries and encryption protocol libraries, wherein the communication between processes of network nodes in the network communication protocol is refined through communication method refinement technology, and the encryption and decryption of communication data is refined through encryption and decryption algorithm refinement technology, thereby improving the accuracy of 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 comprises the following steps:
[0007] 1) Pre-build a model that describes the class of each process to be implemented and its member variables and member methods through UML diagrams, and specifies the types of member methods; also describes the possible states of each process and the transitions between states through finite state automata;
[0008] 2) Pre-build the network communication protocol library CommLib and the encryption protocol library CryptoLib. The CommLib implements simulated communication and real communication between processes based on the network communication interface functions of the data link layer and the transport layer data packet transmission; the CryptoLib implements encryption and decryption of communication data between processes based on the interface functions of symmetric encryption, asymmetric encryption and data signature;
[0009] 3) When generating code, read in the pre-built model and use the model to obtain the data structure required for generating code, 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) According to the file names of the UserType.hpp file, header file and source file, a python script file is generated in the file system, 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 represents a starting state, an ending state, and a code to be executed when switching between states through a triple.
[0014] Further, in step 2), the analog communication includes a synchronous channel and / or an asynchronous channel;
[0015] In real communication, use the monitoring method of libpcap library to monitor the sent custom Ethernet frames to realize Ethernet communication in the local area network; use AF_INET address family and SOCK_DGRAM to send unsecured network datagrams to realize 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, and generate a definition string of the class with the name of UserType as the class name. 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 to the generated code string;
[0020] Create UserType.hpp in the file system and input the concatenated code string into the 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 according to 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, 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 migration behaviors of different states, so as to generate 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 source files in step 5) includes:
[0032] 1) Create an empty string as the generated string;
[0033] 2) 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;
[0034] 3) 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;
[0035] 4) For each method that receives or sends a network data packet in the process, the network protocol type used by the model is obtained, and a code is generated to call 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] The present invention provides a network communication protocol C++ language code generation method based on model refinement under the Linux platform, which can be gradually refined into compilable code according to the model. Developers can use this patent to refine the model and finally generate code to improve development efficiency. The tool provides developers with an automatic protocol code framework implementation by providing two sets of solutions: simulation execution code generation and real environment code generation. The consistency of the model ensures the behavioral consistency of the generated simulation code and the real code, solving the above problems that may arise in the implementation process. 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 obvious and easy to understand, 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 contains a UML diagram that describes the data structure of each process that needs to be implemented. The UML diagram clearly describes the class that implements each process and the member variables and member methods contained in the class. The model also specifies whether the type of the member method is an encryption method, a communication method, or a common method. The model also contains 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 state set and an edge set. 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 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 between the processes of the network nodes in the network communication protocol is refined by the communication method refinement technology, 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 is 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. It implements the network communication interface function that can be based on the data link layer and transport layer packet transmission, and realizes the essential technology of the communication method 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 1, an asynchronous channel stores a list of information and uses a name to identify the identity of the communication node of the channel. Network nodes that use channel communication can communicate by joining and obtaining information in the channel. Synchronous channels are used for synchronous communication. Only when the two communicating processes run the member methods of sending and receiving messages at the same time can they communicate. Otherwise, they will be blocked waiting for the other party to send or receive messages.
[0051] Real communication uses the existing software libraries and socket programming related to Ethernet communication. The libpcap library (network packet capture function library) provides methods for obtaining the sending and listening interfaces of the network card. In the real communication method, the sending method in the libpcap library is used to send broadcast custom Ethernet frames to the local area network. The libpcap library also implements the monitoring and filtering of netfilter (a subsystem used by Linux to manage hook functions). Therefore, the monitoring method of the libpcap library is used to monitor the sent custom Ethernet frames, so that Ethernet communication in the local area network can be realized. If the UDP protocol in real communication is used for communication, when creating a socket, the AF_INET address family and SOCK_DGRAM are used to indicate the sending of unsecured network datagrams, which can realize UDP communication. In the code generation process, if you want to generate code for the sending / receiving method using the UDP communication method in the process, a method implementation containing two functions, sendto and recvfrom, will be generated for synchronous sending and datagrams.
[0052] The process of automatically generating the communication framework of the communication protocol in the subsequent code generation step needs to use the currently known real communication library and the implemented simulation communication library to provide technical solutions for the simulation 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, simulation communication, that is, inter-process communication, is required 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 code generation. Among them, simulation 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 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 in the figure, to use AES encryption and decryption, the data to be transmitted must first be decomposed into multiple plaintext blocks, and then encrypted separately using the AES encryptor. Similarly, for AES decryption at the receiving end of process communication, the encryption key is used to perform the inverse operation to restore the plaintext from the ciphertext block. The AES encryptor has different encryption modes, such as CBC and ECB. The ECB mode directly uses the encryptor to perform bit operations on the original data to obtain the ciphertext block. The CBC mode encrypts in sequence according to the plaintext block order, and uses the encrypted result to encrypt the next plaintext block. The CBC encryption security is better than ECB but is not conducive to parallel computing.
[0056] like Figure 5 As shown, RSA encryption uses a public key-private key pair. The public key encrypts and the private key decrypts. The public key is known to the public. The simplest RSA encryption algorithm involves the following elements:
[0057] N: a large integer N, called the 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. The other is the Euler function value of n, which is often used when solving d.
[0061] The process of generating RSA key 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)) and gcd(φ(n),e) = 1, and calculate d through d*e≡1modφ(n). Then {e,n} is the public key and {d,n} is the secret key.
[0062] Common HASH encryption algorithms include MD5 and SHA series algorithms. Hash encryption algorithm is a one-way irreversible algorithm. Its main function is to generate a message digest for a message of any length after compression through iterative hash functions.
[0063] In the code generated by the present invention, the relevant encryption and decryption interfaces of CryptoLib will be specifically called to perform 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 main function of code generation is to generate and implement the protocol described in the model by reading the data structure in the model, based on the environment provided by the network communication protocol library CommLib and the encryption protocol library CryptoLib for the generated code. Through the code generation process of the present invention, the implementation of the network protocol will be created in the file system, and the specific steps of 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 custom data structure type (UserType) used by the user.
[0066] Step 4: Use the obtained UserType data structure type to 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 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 string defining the class is generated with the name of the UserType as the class name, and 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 to the generated code string, and then a UserType.hpp file will be created in the file system, and the final generated code string will be output to the UserType.hpp file.
[0070] Step 5: Generate the header file and source file described in the output using the obtained Process collection. Specifically, generate the corresponding header file and source file for each process in the Process collection.
[0071] The header file is specifically a C++ header file, each of which corresponds to a process in a network communication node defined in the model. The header file contains and only contains the definition of a class, and the definition of the class is consistent with the definition of the communication node process with the same name described in the model. In addition, the header file also describes the behavior logic of the communication node process (this process) in the main function, which defines the public key and private key information of this process and the public key information of other communication node processes, and implements the different behaviors of this process in different states through a finite state machine, that is, the state migration and behavior logic of this process are realized.
[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 structure 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 to 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 by it 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 of receiving or sending network data packets in the process, the type of network protocol used is obtained through the model, and a section of code that calls the relevant interface in the pre-implemented network communication library is generated. This code, as the implementation of the method of receiving / sending messages, will be spliced after the generated string.
[0084] 5) Create a C++ source file with the current process name in the file system source file folder 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 by the g++ compiler is generated in the file system. The script compiles the generated source file to generate a .o file, which is then linked. A binary executable file is compiled for each process, which can be used for the deployment and communication of the communication node corresponding to the process.
[0086] Experimental test:
[0087] To verify the feasibility of the method of the present invention, this experiment modeled and generated codes for both the Needham protocol and the 802.1x protocol. The experiment shows that in these two instances, the code generation method of the present invention can quickly and correctly generate the implementation corresponding to the protocol, and after compiling the generated code, it can be deployed to the network node and run.
[0088] Although the present invention has been disclosed as above by way of embodiments, it is not intended to limit the present invention. Appropriate modifications or equivalent substitutions of the technical solutions of the present invention made by ordinary technicians in the field should all be included in the protection scope of the present invention. The protection scope of the present invention shall be based on what is 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 and its member variables and member methods through UML diagrams, and specifies the types of member methods; also describes the possible states of each process and the transitions between states through finite state automata; 2) Pre-build the network communication protocol library CommLib and the encryption protocol library CryptoLib. The CommLib implements simulated communication and real communication between processes based on the network communication interface functions of the data link layer and the transport layer data packet transmission; the CryptoLib implements encryption and decryption of communication data between processes based on the interface functions of symmetric encryption, asymmetric encryption and data signature; 3) When generating code, read in the pre-built model and use the model to obtain the data structure required for generating code, 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) According to the file names of the UserType.hpp file, header file and source file, a python script file is generated in the file system, the script compiles the source file, and then compiles each process to generate a binary executable file.
2. The method according to claim 1, characterized in that In step 1), the finite state automaton includes a state set and an edge set. Each state is identified by a unique ID, and each edge is represented by a triple that represents the starting state, the ending state, and the code to be executed when converting between states.
3. The method according to claim 1, characterized in that In step 2), the analog communication includes a synchronous channel and / or an asynchronous channel; In real communication, use the monitoring method of libpcap library to monitor the sent custom Ethernet frames to realize Ethernet communication in the local area network; use AF_INET address family and SOCK_DGRAM to send unsecured network datagrams to realize UDP communication; and / or The encryption algorithms included in CryptoLib include AES, RSA and HASH.
4. The method according to claim 1, characterized in that 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, and generate a definition string of the class with the name of UserType as the class name. 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 to the generated code string; Create UserType.hpp in the file system and input the concatenated code string into the UserType.hpp.
5. The method according to claim 1, characterized in that 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, characterized in that 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, characterized in that 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 according to 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, characterized in that The method of generating code for the finite state machine corresponding to the behavior of the process according to the model is: using integer variables to simulate the state, using while loop structure and switch-case branches to simulate the migration behavior of different states, so as to generate code.
9. The method according to claim 1, characterized in that 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 interfaces and functions provided by CommLib and CryptoLib.
10. The method according to claim 9, characterized in that 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 concatenated to the generated string; For each method in the process that receives or sends network packets, the network protocol type used by the model is obtained, and a code is generated to call 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
Method and device for generating code generation realizing server function invocation
CN108932130A
Restful service development method and device based on SSM framework
CN112748908A
Model construction and code generation method oriented to industrial control network protocol
CN114884827A
System and method for source code generation
US20210089281A1
Cited By
Client code framework construction method, equipment and medium
CN119861967A
