Asynchronous network protocol analysis implementation system and method based on state machine
Through the state machine-based asynchronous network protocol parsing system, the decoder is decoupled from state management, and the state machine is responsible for scheduling, which solves the complexity problem in multi-session concurrent processing and realizes efficient and stable protocol parsing.
Patent Information
- Application Number
- CN202511211878.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-28
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2045-08-28
AI Technical Summary
In the field of network auditing and network security, existing technologies have problems such as complex protocol parsing, chaotic state management, and difficult code maintenance when processing multiple sessions concurrently. Especially when data from the same session arrives asynchronously, the parsing process becomes incoherent and error-prone.
An asynchronous network protocol parsing system based on a state machine is adopted. Through the separate design of the decoder, state table and state machine, the decoder focuses on protocol parsing, the state table records the session status, and the state machine is responsible for scheduling and switching, thus realizing the decoupling of the decoder and state management.
It improves the performance and scalability of protocol parsing, reduces code complexity and maintenance difficulty, improves system stability and scalability, and adapts to high concurrency and complex network environments.
Smart Images

Figure CN120729962A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a state machine-based asynchronous network protocol parsing implementation system and method thereof. Background Art
[0002] In the fields of network auditing and network security, it is necessary to analyze and detect the application layer protocol content of network packets. To obtain the application layer protocol content, the application layer protocol must be restored and parsed. The parsing process is complex. Network security testing programs or devices must process a large number of sessions simultaneously, requiring a single process to handle protocol parsing for multiple sessions simultaneously. However, data for the same session does not arrive all at once. Due to performance requirements, processing the same session cannot wait, as this would result in data loss for other sessions.
[0003] Therefore, the typical protocol parsing process is to process data from each session in the order it arrives. If data from one session does not arrive, the process switches to processing data from another session. During this switching process, the status of the parsing process for the current session is recorded, for example, if the parsing has reached the third field of the protocol header and is not complete. When subsequent packets from this session arrive, the parsing operation continues based on the previously recorded status information.
[0004] In the field of network auditing and network security, it is necessary to analyze and detect the content of the application layer protocol of network packets. This process involves the following core technical issues: Concurrent processing of multiple sessions: Network security detection programs or devices need to process a large number of sessions simultaneously, and one processing process needs to handle protocol parsing of multiple sessions simultaneously.
[0005] Data packets arrive asynchronously: Data for the same session does not arrive all at once, but arrives at different time points.
[0006] High performance requirements: No waiting is allowed while processing the same session, because waiting will cause data loss in other sessions.
[0007] Complex state management: When data from one session does not arrive, it is necessary to switch to processing data from another session. This switching process requires recording the state processed during the parsing of the current session and continuing the parsing operation based on the recorded state information when subsequent data packets arrive. Summary of the Invention
[0008] The existing technology has the following problems: 1. Complex code: The process of state recording, switching, and recovery makes the code very confusing and difficult to read and understand, which easily leads to incoherent error parsing process; 2. Due to the need to switch between different sessions, the protocol parsing process is no longer the natural order of conventional protocol descriptions, but a cross-cutting process; 3. Frequent state switching and saving: The parsing process needs to be constantly interrupted to save the intermediate state, switch to another connection, and then switch back to restore the last saved state. This process is difficult to implement and maintain.
[0009] To achieve the above-mentioned purpose, one of the technical solutions adopted by the present invention is: an asynchronous network protocol parsing implementation system based on a state machine, the system including a decoder, a state table and a state machine; The decoder is used to divide multiple protocol processes according to the protocol description, implement each protocol decoding process separately, and register all decoding processes in the state table; The state table is used to record the status of different stages and different sessions in each protocol decoding process; The state machine is responsible for executing the process, conversion and recovery of the states in the state table and is decoupled from the decoder.
[0010] Another technical solution adopted by the present invention is: a method for implementing asynchronous network protocol analysis based on a state machine, which is used in the above-mentioned asynchronous network protocol analysis implementation system based on a state machine.
[0011] Furthermore, the method includes: Perform state machine initialization process on the state machine; The decoding process of the protocol is divided into processes and registered into the state table process in the decoder; The state machine starts from the initialization state in the state table, calls the parsing function registered on it, and starts the decoding process.
[0012] Furthermore, the state machine initialization process is to set various parameters and values for the state machine and initialize the initial function of the state machine.
[0013] Furthermore, the decoding process of the protocol is divided into a plurality of stages, decoding of each stage is completed, and a transition process between the decoding processes of each protocol is determined.
[0014] Furthermore, registering into the state table is registering the parsing function in each stage into the state table.
[0015] Furthermore, the decoding process includes: if the data packet of the session required by the parsing function has not arrived, returning to the state table to wait; The state machine saves the current state to the state table and turns to execute the state machine of other session links; When a data packet of a session that needs to be parsed arrives, the state machine is retriggered and the corresponding state saved in the state table is extracted for execution; After the current state ends, the state machine loads the next state in the state table and continues execution; The decoding process is repeated until the decoding is completed or an error occurs in the middle, then the process ends.
[0016] Furthermore, the state machine loads the next state in the state table according to the return value.
[0017] Compared to existing technologies, this approach, which relies on an independent universal state machine for network protocol parsing, represents an architectural upgrade compared to current mainstream decoder implementation logic. Its core breakthrough lies in completely decoupling the decoder from state management, freeing the decoder from the tedious work of state recording and switching, allowing it to focus on the protocol's syntax and semantic parsing, thereby triggering a series of cascading advantages.
[0018] In the current implementation logic of traditional decoders, each protocol decoder must not only complete the parsing of protocol fields but also have a built-in state management mechanism. For example, when the SMTP session data is interrupted while being parsed, the decoder needs to define its own state variables to record the interruption point and write switching logic to handle the intervention of other sessions. This parsing logic + state management bundling model leads to two significant problems: First, the decoder code becomes bloated, as the core logic of protocol parsing is overwhelmed by the state processing code, significantly reducing readability. Second, decoders for different protocols need to repeatedly implement similar state switching logic, which not only wastes development resources but also increases the risk of introducing bugs due to logic duplication. For example, if a decoder omits to save the protocol version field during a state switch, it may cause field misalignment during subsequent parsing.
[0019] The new approach completely removes this complexity from the decoder by introducing an independent, universal state machine. This state machine coordinates the scheduling, state recording, and switching of all sessions. When data from session A is temporarily incomplete, the state machine automatically records its parsing progress and switches decoder resources to session B. When subsequent data from session A arrives, the state machine quickly locates the historical state using the session's unique identifier and resets the decoder to the point where it was last interrupted, allowing it to continue parsing from the remaining two bytes of the type field. This process greatly simplifies the protocol decoder's perspective: it doesn't need to worry about which session it's currently processing or whether data will be interrupted. From its perspective, it is always processing a continuous data stream that flows according to the protocol's natural order, just as smoothly as parsing protocol data from a local file. This separation of the synchronous parsing surface from the asynchronous processing core reduces the decoder's code size by over 30%, highlighting the core logic and significantly lowering the barrier to development and maintenance.
[0020] From a performance and scalability perspective, this architecture offers significant advantages. In terms of performance, the universal state machine utilizes an efficient event-driven model, enabling session state switching and loading within milliseconds, avoiding the lock contention and resource blocking associated with traditional decoders' built-in state management. For example, when processing 100,000 concurrent sessions per second, the state machine can quickly locate session information using a pre-allocated state buffer pool. Traditional decoders, however, may allocate and release independent state variables for each session, leading to memory fragmentation and increased garbage collection pressure. Regarding scalability, when adding protocol parsing support, developers only need to focus on the protocol's syntax, eliminating the need to rewrite state recording logic. The state machine has built-in universal interrupt and resume mechanisms, allowing new decoders to seamlessly integrate into a multi-session asynchronous processing environment by simply following the synchronous process from receiving data to parsing fields to returning results. This single-development, multi-scenario reuse capability significantly increases the scalability of the protocol parsing module, making it particularly well-suited for the current era of rapidly evolving network protocols.
[0021] More fundamentally, this approach solves the fragmentation problem of traditional decoders by standardizing the state management interface. In traditional implementations, the state definition of an HTTP decoder might be completely different from that of an FTP decoder, making cross-protocol state monitoring and debugging extremely difficult. However, the universal state machine uses a unified state description format, allowing administrators to view the parsing status of all sessions using the same set of tools, quickly identifying the cause of a session stall due to incomplete protocol headers, and significantly reducing operational complexity.
[0022] In summary, this architecture of state machine coordination and decoder-focused parsing not only improves performance through asynchronous processing but also addresses pain points in traditional implementations, such as code redundancy, poor scalability, and difficult maintenance, through decoupling and standardization. For large-scale network security devices that need to support hundreds of protocols, the benefits of this approach scale linearly with the number of protocols. Each new protocol reduces state management development costs and increases system stability, ultimately achieving the ideal state of centralized control of complex logic and streamlined and efficient core functionality. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 The present invention is a flowchart of a method for implementing asynchronous network protocol parsing based on a state machine. DETAILED DESCRIPTION
[0024] The following will be combined with the accompanying drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the state machine-based asynchronous network protocol parsing implementation method and system provided by the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.
[0025] Example 1 An asynchronous network protocol parsing implementation system based on a state machine, the system includes a decoder, a state table and a state machine; The decoder is used to divide multiple protocol processes according to the protocol description, implement each protocol decoding process separately, and register all decoding processes in the state table.
[0026] Specifically, the core responsibility of the decoder is to focus on the protocol's grammatical rules, breaking down complex protocol flows into a series of independent decoding processes, which are then registered with the system through standardized interfaces. Its design concept is similar to a plug-in architecture, where each protocol decoder is an independent functional unit. It doesn't need to worry about multi-session concurrency or state management, but only needs to focus on parsing protocol fields.
[0027] The decoder's operating modes include: protocol process decomposition: breaking down the complete protocol interaction process into several sequential decoding steps. For example, HTTP can be broken down into three phases: request line parsing, request header parsing, and message body parsing; FTP can be broken down into control connection command parsing, data connection parsing, and response parsing. Each phase has clearly defined input, output, and abort conditions.
[0028] Independent Implementation and Registration: Each decoding process is implemented by a dedicated code module and registered in the state table through a unified registration interface. This plug-and-play design allows new protocols to be added by simply developing the corresponding decoder without modifying the core logic of the state machine or state table.
[0029] Synchronous parsing semantics: From the decoder's perspective, each decoding process is synchronous and continuous. It receives a complete byte stream, extracts fields according to the protocol rules, and returns the parsing result if there is enough data. If there is insufficient data, it returns a marker indicating that further data needs to be received, without recording the current progress.
[0030] The state table is used to record the status of different stages and different sessions in each protocol decoding process.
[0031] Specifically, the state table is the memory center of the entire system, responsible for globally storing the real-time status of all protocols and all sessions during the parsing process, providing data support for state machine scheduling. Like a structured database, it uses sessions and protocol stages as indexes to accurately record the interruption points and context information of each parsing process.
[0032] The core functions of the status table include: Multi-dimensional status records: Each status record contains three layers of key information; Session identifier: such as the TCP five-tuple, such as source IP, source port, destination IP, destination port, and protocol type, used to uniquely distinguish different sessions; Protocol phase: marks the decoding process of the current session; Parsing context: includes parsed field values, unparsed byte buffers, and the expected data stream format for the next stage.
[0033] Efficient Indexing and Updates: The state table utilizes a composite structure combining a hash table and a linked list, using session identifiers as keys to quickly locate state records. This allows for millisecond-level query, insert, and update operations. When a session's parsing phase switches or the context changes, the state table updates the corresponding record in real time, ensuring the state machine always has the latest parsing progress.
[0034] State lifecycle management: To avoid memory overflow, the state table cleans up long-term inactive session states while reserving sufficient cache space for active sessions, balancing resource usage and resolution continuity.
[0035] The state machine is responsible for executing the process, conversion and recovery of the states in the state table and is decoupled from the decoder.
[0036] Specifically, the state machine is the nerve center of the system. By driving the state flow in the state table, it realizes asynchronous scheduling, state switching and interrupt recovery of multi-session parsing, and is completely decoupled from the business logic of the decoder. Its core role is to decide when to process which session, how to process it, and where to continue processing. The state machine's operating mechanism can be divided into three steps: Session triggering and state query: When a new data packet arrives, the state machine first extracts the session identifier and queries the historical state of the session through the state table: If it is a new session, the parsing starts from the initial phase of the protocol and a new record is created in the state table; If the session already exists, read its current protocol phase and parsing context.
[0037] Drive the decoder execution: The state machine obtains the parsing context of the current session from the state table, calls the decoder of the corresponding stage, and passes the unfinished byte buffer. After the decoder completes the partial parsing, it returns two results: If the parsing is complete, the state machine updates the state table and advances the session to the next stage; If parsing is interrupted, the state machine writes the unfinished buffer returned by the decoder and the current stage information into the state table, marking the session as to be resumed.
[0038] Multi-session switching and resource scheduling: When a session is interrupted due to insufficient data, the state machine immediately releases the current processing resources, extracts the next session's data packet from the pending queue, and repeats the process. This round-robin scheduling ensures that all sessions are processed fairly according to the order in which data arrives, avoiding global process blockage due to waiting for a particular session.
[0039] In summary, the overall operation process of the system presents an efficient closed loop: After a network data packet enters the system, the state machine first queries the state table using the session identifier to determine the parsing starting point; the state machine calls the decoder of the corresponding stage and passes in the context data in the state table, such as unfinished buffers; the decoder synchronously parses the data and returns results or interrupt information, and the state machine updates the state table accordingly; the state machine switches to the next session and repeats the above process until all data packets are processed.
[0040] The advantage of this collaborative model is that the decoder focuses on parsing, the state table records the state, and the state machine controls the flow of the process. These three interact through standardized interfaces, ensuring both the simplicity of individual modules and the efficiency of the overall system. For network security devices that need to handle massive concurrent sessions, this architecture can ensure parsing accuracy while reducing performance loss by over 40%, providing solid technical support for real-time threat detection.
[0041] Taking HTTP protocol parsing as an example, the specific implementation process of this method is explained: The decoder definition divides HTTP protocol parsing into three main phases: request URL, request header, and request body. The request URL phase parses the HTTP request method and URL. The request header phase parses the various fields in the HTTP request header. The request body phase parses the HTTP request body.
[0042] The decoder registration registers the parsing functions of the above three stages into the state table of the state machine, forming the following state transition: request URL to request header to request body.
[0043] State Machine Operation - At the start of the state machine, the state machine begins execution in the Request URL state. If the received data is insufficient to complete URL parsing, the state machine saves the current state and moves on to other sessions. When the remaining URL data arrives, the state machine resumes its previous state, completes URL parsing, and then enters the Request Header state. Request header parsing may require multiple packets. The state machine saves its state each time insufficient data is available and resumes parsing when new data arrives. After request header parsing is complete, the state machine enters the Request Body state and begins parsing the HTTP request body. If any errors occur during this process, the state machine terminates processing of the current session.
[0044] In this way, the HTTP protocol parsing process is decomposed into multiple independent states, each of which can be executed and resumed independently, thus realizing the ability to handle multiple HTTP sessions asynchronously while maintaining code clarity and maintainability.
[0045] Example 2 like Figure 1 As shown, a state machine-based asynchronous network protocol parsing implementation method is used in the state machine-based asynchronous network protocol parsing implementation system recorded in the above embodiment 1.
[0046] Furthermore, the method includes: performing a state machine initialization process on the state machine; Furthermore, the state machine initialization process is to set various parameters and values for the state machine and initialize the initial function of the state machine.
[0047] Specifically, the state machine initialization process is the critical transition from standby to operational state. By presetting core parameters, configuring operational rules, and loading initial functions, it establishes a stable underlying framework for subsequent multi-session protocol parsing. This process, like a calibration benchmark for precision instruments, directly determines the system's stability and efficiency in high-concurrency, high-throughput scenarios.
[0048] The core parameters that need to be set explicitly during the initialization phase include: Session Cap: This defines the maximum number of sessions the system can maintain simultaneously to avoid crashes due to memory overflow. The state machine pre-allocates hash table slots and state record structures based on this, ensuring that new sessions can be quickly inserted.
[0049] State Timeout Threshold: Set the maximum wait time for a session to resume without data. Session states exceeding this threshold will be automatically cleared, freeing up memory resources and preventing long-term idle state records from taking up space.
[0050] Buffer size: Configure the maximum buffer size for each session to ensure that it can accommodate key data such as protocol headers and commands while avoiding memory waste due to an overly large buffer.
[0051] Protocol type mapping table: establishes the association between protocol identifiers and decoders to ensure that the state machine can quickly locate the corresponding parsing module.
[0052] These parameters are like the system's operating instructions, which not only limit the upper limit of resource consumption but also provide a clear basis for judging state flow.
[0053] The initialization process requires calling and registering three key functions, which constitute the nerve center of the state machine: Session identification function: This function is responsible for extracting the five-tuple from the data packet and generating a unique session identifier, which serves as the index for state table queries. This function must be loaded during initialization to ensure that all data packets are correctly classified.
[0054] State transition driver function: This function defines the flow rules from parsing completion to entering the next stage of parsing interruption and then saving the state. For example, when the HTTP request line is parsed, this function automatically triggers a state table update, advancing the session to the request header parsing stage.
[0055] Resource Scheduling Function: Implements multi-session prioritization and processing queue management. For example, it prioritizes sessions with sensitive ports, ensuring that encrypted traffic such as HTTPS is parsed first, reducing latency.
[0056] These functions are loaded into memory during initialization and bound to the event trigger points of the state machine, becoming the beginning of the driving process.
[0057] In summary, initializing the state machine eliminates runtime uncertainty. Dynamic network environments, such as traffic bursts or mixed protocols, require the system to clearly define boundary conditions during startup. For example, if a session capacity cap isn't preset, when the number of instantaneous sessions exceeds memory capacity, the state table may become overwhelmed due to a surge in hash conflicts. Without a timeout cleanup mechanism, a large number of zombie sessions will continue to occupy state records, preventing new sessions from being processed. Initialization, by solidifying parameters, constrains system behavior within a controllable range, preventing chaos caused by irregularities during runtime.
[0058] At the same time, reducing runtime computational overhead and pre-loading parameters and functions can significantly reduce dynamic decision-making costs at runtime: the pre-allocated session hash table can reduce state query time from O(n) to O(1), avoiding performance fluctuations caused by dynamic expansion during high concurrency; the pre-registered protocol and decoder mapping table eliminates the process of determining the protocol type before each parsing and searching for the decoder, directly locating the parsing module by port or identifier, improving response speed. This pre-computation mode allows the state machine to maintain microsecond single-operation latency when processing millions of packets per second.
[0059] This also lays the foundation for system scalability. The modular design of the initialization process, such as parameter configuration files or function registration interfaces, allows the system to expand functionality by modifying the configuration. When adding a new protocol, simply add the protocol identifier to the decoder in the mapping table, without modifying the core state machine logic. When adjusting performance parameters, simply modify the initialization configuration file and restart, without refactoring the code. This configuration-driven scalability enables the system to quickly adapt to new protocols or higher-bandwidth network environments.
[0060] The state machine initialization process is the first line of defense for system stability. Rather than simply assigning parameters, it establishes a controllable, efficient, and scalable operating foundation for the entire system through pre-set rules, solidified logic, and optimized resources. This process not only avoids runtime crashes caused by uncontrolled resources, but also improves the real-time nature of protocol parsing through a preloading mechanism. It also makes expansion requirements such as adding new protocols and adjusting performance simple and controllable. For network security devices that must operate 24 / 7, this process is a prerequisite for ensuring high availability. Only by laying a solid foundation during the initialization phase can the state machine accurately schedule decoders and state tables in complex network environments, achieving efficient and stable multi-session protocol parsing.
[0061] The decoding process of the protocol is divided into processes and registered into the state table process in the decoder; Furthermore, the decoding process of the protocol is divided into a plurality of stages, decoding of each stage is completed, and a transition process between the decoding processes of each protocol is determined.
[0062] Furthermore, registering into the state table is registering the parsing function in each stage into the state table.
[0063] Specifically, in a state-machine-based asynchronous network protocol parsing system, the core work of the decoder lies not only in parsing the protocol fields but also in enabling the efficient scheduling of each protocol's parsing logic by the state machine through structured process partitioning and standardized state mechanisms. These two processes act like building blocks for protocol parsing, first breaking down complex processes into reusable modules, then connecting these modules to the system's scheduling hub, ultimately achieving a complete decoupling of parsing logic and state management.
[0064] The division of the protocol decoding process is essentially a structured decomposition of the protocol interaction logic. By clarifying the input, output, and flow conditions of each stage, complex protocol parsing becomes divisible, interruptible, and resumable.
[0065] The decoding process of each protocol needs to be divided into several consecutive and independent stages, each of which focuses on parsing a certain functional block of the protocol.
[0066] Taking the HTTP protocol as an example, the decoder can divide the parsing process into three phases: the request URL, the request header, and the request body. Each phase implements its own parsing logic. For example, the request URL phase parses the HTTP method and URL, the request header phase parses the individual header fields, and the request body phase parses the content. This division makes the implementation of each phase relatively simple and easy to modify or extend based on protocol changes.
[0067] The core principle of stage division is data integrity boundaries: the parsing of each stage relies on a continuous and independently verifiable piece of data. For example, the HTTP request line ends with \r\n. This ensures that when insufficient data is available, the current stage can be interrupted and continued from the same stage when subsequent data arrives.
[0068] After dividing the phases, it is necessary to clarify the triggering conditions and flow paths between the phases to ensure that the parsing process proceeds in an orderly manner according to the protocol logic. The core of the transition process is the completion of the current phase to trigger the next phase, which specifically includes: Normal transition: When the current phase is parsed and the data is complete, it automatically enters the next phase. For example, after the HTTP request line is parsed, for example, if \r\n is detected, it can enter the request header parsing phase without additional conditions.
[0069] Conditional transition: The next phase is determined based on the parsing result of the current phase. For example, in the FTP protocol, if the response 331Passwordrequired is parsed in phase 2, the next phase must be the PASS command parsing; if the response 530Notloggedin is parsed, the connection close phase is directly entered.
[0070] Abnormal transition: When a protocol error is parsed, the termination phase is triggered and parsing is discontinued to avoid invalid resource consumption.
[0071] These transition rules are encoded as the decoder's stage-jumping logic, ensuring that the end of each stage clearly indicates what to do next.
[0072] After the phase division is complete, the parsing function for each phase must be registered in the state table, allowing the state machine to accurately call the corresponding parsing logic based on the phase information of the current session. This process essentially establishes a mapping relationship between protocol type + phase number and parsing function.
[0073] The parsing logic of each stage needs to be encapsulated as an independent function and follow a unified interface specification: Input: the byte stream to be parsed at the current stage; Output: parsing results, such as extracted field values and structured data; status identification, such as parsing completion, insufficient data requiring interruption, or protocol error; next stage number.
[0074] For example, the HTTP phase 1 parsing function http_parse_request_line() receives a byte stream and attempts to match the format of METHODURLVERSION\r\n. If successful, it returns the parsed method / URL / version and specifies that the next phase is request header parsing. If the byte stream only contains GET / index.ht, it returns an insufficient data indicator and retains the current buffer.
[0075] The parsing function is bound through the registration interface of the state table. For example, the register_stage_parser(protocol_id, stage_id, parser_func) function is called to associate the protocol ID + stage ID with the parsing function and store it in the parsing function registry of the state table.
[0076] For example: register http_parse_request_line() for phase 1 of the HTTP protocol; Registers ftp_parse_auth() for phase 2 of the FTP protocol.
[0077] The state table maintains a phase-function mapping table for each protocol. When the state machine needs to parse a phase of a session, it can quickly query and call the corresponding parsing function by simply using the protocol ID and the current phase ID, without having to worry about the specific implementation of the function.
[0078] Modularizing the protocol parsing logic reduces development complexity. By splitting the stages, developers no longer need to implement the entire protocol parsing logic all at once, focusing on the functionality of a single stage. This reduces the code size of a single module by over 60%, resulting in clearer logic and easier debugging. This clear transition process avoids the problem of a single change affecting the entire system: when modifying the parsing logic of a particular stage, only the corresponding stage function needs to be adjusted, without affecting the scheduling logic of other stages or the state machine.
[0079] The system supports asynchronous state machine continuation, enabling non-blocking parsing. After phase division, the parsing progress of each session can be accurately described by the current phase ID + buffer. When data is interrupted, the state machine only needs to record these two pieces of information, eliminating the need to store complex parsing context, significantly simplifying state recording. The design of parsing completion to the specified next phase eliminates the need for the state machine to understand protocol logic and simply advances the state based on the next phase number returned by the parsing function, completely decoupling parsing logic from state management.
[0080] Improve system scalability and adapt to multi-protocol scenarios. When adding a new protocol, simply divide the phases according to the rules, implement the parsing functions for each phase, and register them in the state table, without modifying the core state machine logic. For example, when supporting the WebSocket protocol, simply split the handshake phase into the frame parsing phase and the closing phase, and register the corresponding functions. The system will automatically identify and schedule them. When upgrading the protocol, simply modify the parsing functions for the corresponding phases and re-register them. The parsing logic of the old protocol remains compatible, ensuring a smooth transition.
[0081] Enhanced observability of the parsing process facilitates troubleshooting. Phased parsing allows the status of each session to be accurately described. Combined with the records in the state table, developers can intuitively track the location of parsing interruptions, quickly locate problems, and significantly reduce the difficulty of debugging. Through structured splitting and standardized registration, these two processes transform protocol parsing from a chaotic overall logic into controllable phase modules, and enable them to be efficiently scheduled by the state machine. For developers, this means no need to worry about the complexity of multi-session asynchronous processing, but only need to focus on the parsing logic of a single phase; for the system, this means strong scalability and stability, and the ability to support new protocols and adapt to new scenarios without modifying the core architecture. This design of each doing its own thing is the core reason why the state machine-based parsing system can efficiently handle multi-session asynchronous parsing.
[0082] At the same time, the state table decouples the decoder and state machine, improving the modularity of the system. It records the different stages and session states of the protocol decoding process, allowing the state machine to efficiently manage and switch the parsing state of different sessions, thus realizing asynchronous processing capabilities.
[0083] The state table can be designed as a data structure containing multiple fields. For example: {session_id: unique identifier, protocol: HTTP, current_state: request header, next_state: request body, parsed_data: {…}, remaining_data: …last_execution_point: 1234}.
[0084] This design allows the state machine to quickly find and restore the parsing state for a particular session, while providing a clear interface for the decoder to update the parsing progress.
[0085] The state machine starts from the initialization state in the state table, calls the parsing function registered on it, and starts the decoding process.
[0086] Furthermore, the decoding process includes: if the data packet of the session required by the parsing function has not arrived, returning to the state table to wait; The state machine saves the current state to the state table and turns to execute the state machine of other session links; When a data packet of a session that needs to be parsed arrives, the state machine is retriggered and the corresponding state saved in the state table is extracted for execution; After the current state ends, the state machine loads the next state in the state table and continues execution; specifically, the state machine loads the next state in the state table according to the return value.
[0087] The decoding process is repeated until the decoding is completed or an error occurs in the middle, then the process ends.
[0088] Specifically, after the state machine is started, it starts from the initialization state of the state table, calls the corresponding parsing function according to the registered mapping relationship, and enters the decoding process.
[0089] Parsing interruption and state saving: To avoid meaningless waiting, when the parsing function detects that the data packet of the current session is incomplete, it will immediately return an insufficient data flag to the state machine. At this time: the state machine triggers the state saving mechanism and writes the core information of the current session into the state table: including the current stage number, unfinished byte buffer, and parsed temporary results. The state machine releases the processing resources of the session, extracts the data packet of the next session from the queue to be processed, and calls the parsing function of its corresponding stage. For example, after processing the interruption of session A, it immediately switches to the FTP command parsing stage of session B to ensure that CPU resources are not idle. This mechanism of switching immediately without waiting for data fundamentally avoids the problem of global blocking caused by waiting for a single session in traditional synchronous decoding.
[0090] Session continuation and state recovery: seamless parsing process. When a subsequent data packet of a session arrives: the network interface triggers a data packet arrival event, and the state machine quickly locates the historical state record of the session in the state table through the session identifier. The state machine restores the context: extracts the saved stage number and unfinished buffer, and splices the newly arrived byte stream into the buffer to form a complete data to be parsed. The state machine calls the parsing function of this stage and continues parsing from the last interruption point. Since the context is complete, the parsing function will complete the remaining parsing of the request header as if it had never been interrupted, and return the parsing completion identifier. This process realizes the seamless connection between interruption and continuation. For the parsing function, it is as if it is always processing a continuous data stream and is completely unaware of the existence of multi-session switching.
[0091] Stage advancement and next-stage loading: Automatic flow according to rules. After the current stage's parsing is complete, the parsing function returns a parsing success flag, along with the next stage number. Based on this, the state machine updates the session's stage information in the state table, changing the current stage from request header parsing to message body parsing and clearing the temporary buffer for the completed stage. Based on the new stage number, the parsing function for the next stage is called from the mapping relationship in the state table, passing in a new data packet or waiting for subsequent data from that stage. If the parsing function returns a protocol error, the state machine marks the session as terminated, discontinues parsing, and records the error type in the state table for subsequent auditing and troubleshooting.
[0092] Iterates until decoding is complete or interrupted. The above process repeats: from the state machine processing session A, to insufficient data, to saving the state, to switching, to session B, to session B parsing completion, to advancing to the next stage, to new data arriving from session A, to resuming the parsing state, until the decoding process for all sessions is completed or terminated due to protocol errors, timeouts, etc.
[0093] In summary, this maximizes CPU utilization and avoids resource waste. In traditional synchronous decoding, if a session is waiting due to insufficient data, the entire processing thread is blocked, causing the CPU to idle. However, this process uses state preservation and instant switching to ensure that the CPU is always processing valid data. When session A is interrupted, the state machine immediately schedules the parsing of session B, maintaining high CPU utilization. In high-concurrency scenarios, this mechanism can reduce processing latency from milliseconds to microseconds, meeting real-time detection requirements.
[0094] Ensures parsing continuity and eliminates the risk of data loss. The state table's complete record of session status ensures that the parsing process for each session is not interrupted by handoffs. Even if a session's data packets arrive 10 seconds apart, the state machine can still extract historical information from the state table and resume parsing from the last interruption point, avoiding the problem of traditional data fragmentation loss leading to parsing failures. For network anomalies such as TCP retransmissions and out-of-order transmissions, the state machine can automatically correct the data sequence through buffer splicing and state continuation to ensure accurate protocol parsing.
[0095] Simplify parsing function design and reduce development costs. Parsing functions no longer require built-in state management logic; they only need to focus on whether the current data is parseable and how to extract fields, significantly reducing development difficulty. Taking HTTP parsing as an example, traditional implementations require 200 lines of code to handle state switching, while this process only requires 80 lines of code focused on field extraction, a 60% reduction in code volume. When integrating new protocols, developers only need to implement parsing functions at each stage according to the rules, without having to worry about multi-session scheduling. The onboarding period is shortened from weeks to days.
[0096] In summary, the decoding process improves system stability and maintainability. Decoupling the state machine from the parsing function simplifies system fault location. If a session parsing anomaly occurs, the state table can be used to query its historical state to quickly identify whether it's a parsing function logic error or a data transmission anomaly. Centralized state management in the state table avoids state inconsistencies caused by traditional independent state maintenance for each parsing function, such as conflicting stage numbers recorded in different functions for the same session.
[0097] Adapting to complex network environments and enhancing robustness. Fragmentation and disorganization of network data are common, and this process naturally adapts to these characteristics through a state preservation and reconnection mechanism. For TCP fragmentation caused by MTU limitations, the state machine can achieve complete parsing through buffer splicing and stage reconnection. To address sudden traffic shocks, the state machine automatically releases invalid session resources through a state table timeout cleanup mechanism, ensuring that core service parsing is not affected.
[0098] By automatically saving and restoring the context, the state machine can proactively release CPU resources when data is insufficient and switch to processing other sessions, thereby improving overall processing efficiency. This mechanism eliminates the need for the decoder to manually manage complex state switching logic, greatly simplifying the implementation of protocol parsing. When parsing the HTTP request header, if the received data is insufficient to complete the parsing of all header fields, the state machine will automatically save the current parsing state, including information such as the parsed header fields and the current parsing position, to the state table. The state machine can then switch to processing other sessions. When new data arrives, the state machine will restore the previously saved state from the state table and continue parsing from the last interruption, without the decoder having to handle these complex state management logic.
[0099] At the same time, viewing the network protocol as a series of state transitions allows the complex protocol parsing process to be broken down into a series of simple state transitions. This approach makes the protocol parsing process clearer, easier to understand and implement. At the same time, it also provides a theoretical basis for asynchronous processing, allowing the state machine to more naturally manage the transitions between different states. Taking the simplified HTTP protocol as an example, it can be defined as the following state machine: 1. Initial state to parsing the request line; 2. Parsing the request line to parsing the header field; 3. Parsing the header field to parsing the message body; 4. Parsing the message body to the end state. Between each state, it may be paused due to insufficient data, waiting for more data to arrive. This definition makes the entire parsing process very clear, and each state has clear entry and exit conditions.
[0100] The above is a detailed description of an embodiment of the present invention, but the content is only a preferred embodiment of the present invention and should not be considered to limit the scope of the present invention. All equivalent changes and improvements made within the scope of the present invention should still fall within the scope of the patent coverage of the present invention.
Claims
1. Asynchronous network protocol parsing implementation system based on state machine, characterized by: The system includes a decoder, a state table, and a state machine; The decoder is used to divide multiple protocol processes according to the protocol description, implement each protocol decoding process separately, and register all decoding processes in the state table; The state table is used to record the status of different stages and different sessions in each protocol decoding process; The state machine is responsible for executing the process, conversion and recovery of the states in the state table and is decoupled from the decoder.
2. A method for implementing asynchronous network protocol parsing based on a state machine, characterized in that: The method is used in the state machine-based asynchronous network protocol analysis implementation system described in claim 1.
3. The method for implementing asynchronous network protocol parsing based on a state machine according to claim 2, wherein: The method includes: Perform state machine initialization process on the state machine; The decoding process of the protocol is divided into processes and registered into the state table process in the decoder; The state machine starts from the initialization state in the state table, calls the parsing function registered on it, and starts the decoding process.
4. The method for implementing asynchronous network protocol parsing based on a state machine according to claim 3, wherein: The state machine initialization process is to set various parameters and values for the state machine and initialize the initial function of the state machine.
5. The method for implementing asynchronous network protocol parsing based on a state machine according to claim 4, characterized in that: The decoding process of the protocol is divided into multiple stages, the decoding of each stage is completed, and the transition process between the decoding processes of each protocol is determined.
6. The method for implementing asynchronous network protocol parsing based on a state machine according to claim 5, characterized in that: Registering to the state table means registering the parsing function in each stage into the state table.
7. The method for implementing asynchronous network protocol parsing based on a state machine according to claim 3, wherein: The decoding process includes: if the data packet of the session required by the parsing function has not arrived, then return to the state table to wait; The state machine saves the current state to the state table and turns to execute the state machine of other session links; When a data packet of a session that needs to be parsed arrives, the state machine is retriggered and the corresponding state saved in the state table is extracted for execution; After the current state ends, the state machine loads the next state in the state table and continues execution; The decoding process is repeated until the decoding is completed or an error occurs in the middle, then the process ends.
8. The method for implementing asynchronous network protocol parsing based on a state machine according to claim 4, wherein: The state machine loads the next state in the state table based on the return value.
Citation Information
Patent Citations
Method and device for decoding network protocol
CN102075512A
Method and device for protocol resolution
CN102916967A
Run-length decoding digital circuit
CN112600566A
Protocol emulator
US20020156885A1