-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_parser.h
More file actions
103 lines (90 loc) · 2.95 KB
/
Copy pathpacket_parser.h
File metadata and controls
103 lines (90 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// Created by mark on 25-1-10.
// 数据库流量解析系统 - 数据包解析器
//
#ifndef DATABASE_TRAFFIC_PARSER_PACKET_PARSER_H
#define DATABASE_TRAFFIC_PARSER_PACKET_PARSER_H
#include "Packet.h"
#include "constants.h"
#include "message_decoder.h"
#include "tcp_client.h"
#include <arpa/inet.h>
#include <iostream>
#include <cstdint>
#include <string>
#include <PcapFileDevice.h>
#include <map>
#include <utility>
#include <TcpLayer.h>
#include <cstring>
#include <memory>
/**
* @brief 数据包解析器类,负责解析PCAP文件并转发数据库流量
*/
class PacketParser {
public:
/**
* @brief 构造函数
* @param config 解析器配置
* @throws std::runtime_error 当初始化失败时抛出异常
*/
explicit PacketParser(const ParserConfig& config);
/**
* @brief 析构函数
*/
~PacketParser();
/**
* @brief 获取流的唯一ID
* @param src_ip 源IP地址
* @param dst_ip 目标IP地址
* @param src_port 源端口
* @param dst_port 目标端口
* @return 流ID
*/
StreamId GetStreamId(const pcpp::IPAddress& src_ip, const pcpp::IPAddress& dst_ip,
uint16_t src_port, uint16_t dst_port) const;
/**
* @brief 读取并发送数据包
*/
void ReadAndSendPackets();
/**
* @brief 将分片数据推入缓冲区
* @param parsed_packet 解析的数据包
* @param stream_id 流ID
*/
void PushToBuffer(pcpp::Packet& parsed_packet, StreamId stream_id);
private:
/**
* @brief 构建会话启动消息
* @return 启动消息结构
*/
[[nodiscard]] SessionStartupMessage BuildStartupMessage() const;
/**
* @brief 构建会话结束消息
* @return 结束消息结构
*/
[[nodiscard]] SessionGenericMessage BuildEndMessage() const;
/**
* @brief 构建通用会话消息
* @param from_server 是否来自服务器
* @param msg_length 消息长度
* @param offset 数据偏移量
* @return 通用消息结构指针(需要手动释放)
*/
[[nodiscard]] SessionGenericMessage* BuildGenericMessage(bool from_server,
size_t msg_length,
size_t offset) const;
/**
* @brief 清理流状态
* @param stream_id 流ID
*/
void CleanupStream(StreamId stream_id);
private:
ParserConfig config_; // 解析器配置
std::unique_ptr<pcpp::IFileReaderDevice> pcap_reader_; // PCAP文件读取器
MessageLengthDecoder message_decoder_; // 消息长度解码器
std::shared_ptr<pcpp::RawPacket> current_raw_packet_; // 当前原始数据包
TcpClient tcp_client_; // TCP客户端
StreamId current_stream_id_; // 当前流ID
};
#endif // DATABASE_TRAFFIC_PARSER_PACKET_PARSER_H