-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.h
More file actions
109 lines (94 loc) · 3.81 KB
/
Copy pathconstants.h
File metadata and controls
109 lines (94 loc) · 3.81 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
104
105
106
107
108
109
//
// Created by lyw on 1/13/25.
// 数据库流量解析系统 - 常量定义和数据结构
//
#ifndef DATABASE_TRAFFIC_PARSER_CONSTANTS_H
#define DATABASE_TRAFFIC_PARSER_CONSTANTS_H
#include "Packet.h"
#include "IPLayer.h"
#include "TcpLayer.h"
#include <PcapFileDevice.h>
#include <arpa/inet.h>
#include <iostream>
#include <cstdint>
#include <string>
#include <map>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <vector>
// 会话消息类型定义
#define SESSION_STARTUP 's' // 启动消息,告知异步模块准备接收复制的流量并进行解析
#define SESSION_COPY_CLIENT 'u' // 流量复制(client to server)
#define SESSION_COPY_SERVER 'd' // 流量复制(server to client)
#define SESSION_END 'e' // 结束消息,告知异步解析模块停止解析并释放相关资源
// 数据库类型枚举
enum DatabaseType {
DB_MYSQL = 1,
DB_ORACLE = 2,
DB_POSTGRESQL = 3,
DB_HIVE = 4,
DB_DAMENG = 5,
DB_SYBASE = 6
};
#pragma pack(push, 1) // 禁止内存对齐,1字节对齐
// 通用会话消息结构
struct SessionGenericMessage {
uint8_t type; // 消息类型
uint32_t length; // 载荷长度,从length字段开始的数据长度,最小值为13
uint64_t flow_id; // 数据库会话的流ID,用于区分不同会话的流量复制消息
uint64_t real_timestamp; // 消息生成时的时间戳
uint8_t payload[]; // 变长载荷:启动消息包含数据库代理元数据,
// 流量复制消息包含数据库会话镜像流量,结束消息为空
};
// 会话启动消息结构
struct SessionStartupMessage {
uint8_t type; // 消息类型
uint32_t length; // 消息长度
uint64_t flow_id; // 流ID
int64_t real_timestamp; // 时间戳
uint32_t client_ip; // 客户端IP
uint16_t client_port; // 客户端端口
uint32_t proxy_ip; // 代理IP
uint16_t proxy_port; // 代理端口
uint32_t upstream_ip; // 上游服务器IP
uint16_t upstream_port; // 上游服务器端口
uint8_t db_type; // 数据库类型
};
#pragma pack(pop)
// 消息大小常量
constexpr size_t SESSION_GENERIC_MESSAGE_SIZE = sizeof(SessionGenericMessage);
constexpr size_t SESSION_STARTUP_MESSAGE_SIZE = sizeof(SessionStartupMessage);
// 流ID类型定义
typedef int StreamId;
// 数据流状态结构
struct DataStream {
int stream_id; // 流ID
int session_status; // 会话状态:0-建立阶段,1-会话阶段
bool certification_flag; // 认证标志
int is_split_message; // 是否为拆分消息标志
bool syn_ack_received; // SYN+ACK是否已接收
};
// 流缓冲区结构,用于重组分片消息
struct StreamBuffer {
uint32_t total_length; // 消息总长度
uint32_t already_read_length; // 已读取长度
std::vector<uint8_t> buffer; // 数据缓冲区
bool completed; // 是否完成重组
};
// 全局流状态映射(在实际项目中应该封装在类中)
extern std::unordered_map<StreamId, DataStream*> g_stream_map;
extern std::unordered_map<StreamId, StreamBuffer> g_stream_buffers;
// 解析器配置结构
struct ParserConfig {
DatabaseType db_type = DB_MYSQL; // 数据库类型
std::string pcap_file; // PCAP文件路径
std::string bpf_filter; // BPF过滤器
uint32_t server_addr = 0; // 服务器地址
uint16_t server_port = 0; // 服务器端口
uint32_t client_addr = 0; // 客户端地址
uint16_t client_port = 0; // 客户端端口
const char* parser_addr = nullptr; // 解析器地址
uint16_t parser_port = 0; // 解析器端口
};
#endif // DATABASE_TRAFFIC_PARSER_CONSTANTS_H