execve_hook/src/socket_protocol.h

114 lines
4.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef SOCKET_PROTOCOL_H
#define SOCKET_PROTOCOL_H
#include <stdint.h>
#include <sys/ioctl.h>
#include <termios.h>
#ifdef HAVE_LZ4
#include "compression.h"
#endif
// 消息类型枚举
typedef enum {
MSG_TYPE_INIT = 1, // 初始化连接,发送命令信息
MSG_TYPE_WINDOW_SIZE_UPDATE = 2, // 终端窗口大小更新
MSG_TYPE_SERVER_RESPONSE = 3, // 服务器响应消息
MSG_TYPE_CLOSE = 4, // 关闭连接
MSG_TYPE_TERMINAL_INPUT = 5, // 终端输入数据(键盘、鼠标等)
MSG_TYPE_TERMINAL_OUTPUT = 6, // 终端输出数据
MSG_TYPE_MOUSE_EVENT = 7, // 鼠标事件
MSG_TYPE_KEY_EVENT = 8 // 键盘事件
} MessageType;
// 消息头结构(固定大小)
// reserved 字段现在用于存储压缩标志:
// bit 0: MSG_FLAG_COMPRESSED - 载荷已压缩
// bit 1: MSG_FLAG_COMPRESS_LZ4 - 使用 LZ4 压缩
// bit 2: MSG_FLAG_COMPRESS_HC - 使用高压缩比模式
// 高 16 位: 压缩前的原始大小(用于解压缓冲区分配)
typedef struct {
uint32_t magic; // 魔数,用于验证 0x42534D54 ("BSMT")
uint32_t type; // 消息类型
uint32_t payload_len; // 载荷长度(压缩后)
uint32_t reserved; // 低 16 位: 压缩标志; 高 16 位: 原始大小/256
} __attribute__((packed)) MessageHeader;
// 从 reserved 字段提取压缩标志和原始大小
#define GET_COMPRESS_FLAGS(reserved) ((reserved) & 0xFFFF)
#define GET_ORIGINAL_SIZE_HINT(reserved) (((reserved) >> 16) * 256)
#define MAKE_RESERVED(flags, orig_size) (((flags) & 0xFFFF) | (((orig_size) / 256) << 16))
// 终端信息结构
typedef struct {
uint32_t is_tty; // 是否为TTY
uint16_t rows; // 行数
uint16_t cols; // 列数
uint16_t x_pixel; // X像素
uint16_t y_pixel; // Y像素
uint32_t has_termios; // 是否有termios属性
uint32_t input_flags; // termios输入标志
uint32_t output_flags; // termios输出标志
uint32_t control_flags; // termios控制标志
uint32_t local_flags; // termios本地标志
} __attribute__((packed)) TerminalInfoFixed;
// 鼠标事件类型
typedef enum {
MOUSE_BUTTON_PRESS = 1,
MOUSE_BUTTON_RELEASE = 2,
MOUSE_MOVE = 3,
MOUSE_SCROLL_UP = 4,
MOUSE_SCROLL_DOWN = 5
} MouseEventType;
// 鼠标事件结构
typedef struct {
uint32_t event_type; // MouseEventType
uint32_t button; // 鼠标按钮1=左键2=中键3=右键)
uint32_t x; // X坐标
uint32_t y; // Y坐标
uint32_t modifiers; // 修饰键Shift, Ctrl, Alt等
} __attribute__((packed)) MouseEvent;
// 键盘事件结构
typedef struct {
uint32_t key_code; // 键码
uint32_t modifiers; // 修饰键
uint32_t is_press; // 1=按下0=释放
} __attribute__((packed)) KeyEvent;
// 魔数定义
#define MESSAGE_MAGIC 0x42534D54 // "BSMT"
#ifdef HAVE_LZ4
// 协议上下文(包含压缩状态)
typedef struct {
CompressionContext compress_ctx; // 压缩上下文
int compression_enabled; // 是否启用压缩
} ProtocolContext;
// 初始化协议上下文
void protocol_init(ProtocolContext* ctx, CompressionType compress_type, int compress_level);
// 带压缩支持的消息读写
int write_message_compressed(int sock, ProtocolContext* ctx, MessageType type,
const void* payload, uint32_t payload_len);
int read_message_decompressed(int sock, MessageType* type, void** payload,
uint32_t* payload_len, uint32_t* original_len);
#endif
// 函数声明
int write_message(int sock, MessageType type, const void* payload, uint32_t payload_len);
int read_message(int sock, MessageType* type, void** payload, uint32_t* payload_len);
void free_message_payload(void* payload);
// 终端输入捕获相关函数
int setup_terminal_raw_mode(int fd);
int restore_terminal_mode(int fd);
int enable_mouse_tracking(int fd);
int disable_mouse_tracking(int fd);
#endif // SOCKET_PROTOCOL_H