|
@@ -35,6 +35,15 @@
|
|
#define VMP_TID_MAX 64
|
|
#define VMP_TID_MAX 64
|
|
#define VMP_TX_POOL_SIZE 64
|
|
#define VMP_TX_POOL_SIZE 64
|
|
#define VMP_STRBUF_LEN 256
|
|
#define VMP_STRBUF_LEN 256
|
|
|
|
+#define MAX_PORTS 16 // 最大端口数量
|
|
|
|
+#define MAX_CONNECTIONS 64 // 最大连接数
|
|
|
|
+
|
|
|
|
+/* 消息类型 */
|
|
|
|
+#define MSG_PORT_UPDATE 1 // 端口更新消息
|
|
|
|
+#define MSG_NEW_CONNECTION 2 // 新连接消息
|
|
|
|
+#define MSG_DATA 3 // 数据传输消息
|
|
|
|
+#define MSG_CLOSE_CONNECTION 4 // 关闭连接消息
|
|
|
|
+#define MSG_HANDSHAKE 5 // 握手消息
|
|
|
|
|
|
typedef struct {
|
|
typedef struct {
|
|
short width;
|
|
short width;
|
|
@@ -81,8 +90,47 @@ typedef struct {
|
|
int sstdout_len;
|
|
int sstdout_len;
|
|
} tx_command;
|
|
} tx_command;
|
|
|
|
|
|
|
|
+/* 消息头结构 */
|
|
|
|
+typedef struct {
|
|
|
|
+ int type; // 消息类型
|
|
|
|
+ int connection_id; // 连接ID
|
|
|
|
+ int port; // 端口号
|
|
|
|
+ int data_size; // 数据大小
|
|
|
|
+ int target; // 接收方标识(0=host, 1=client)
|
|
|
|
+} MessageHeader;
|
|
|
|
+
|
|
|
|
+/* 数据传输消息结构 */
|
|
|
|
+typedef struct {
|
|
|
|
+ MessageHeader header; // 消息头
|
|
|
|
+ char data[MAX_BUFFER_SIZE]; // 数据
|
|
|
|
+} DataMessage;
|
|
|
|
+
|
|
|
|
+/* 端口更新消息结构 */
|
|
|
|
+typedef struct {
|
|
|
|
+ MessageHeader header; // 消息头
|
|
|
|
+ int num_ports; // 端口数量
|
|
|
|
+ int ports[MAX_PORTS]; // 端口列表
|
|
|
|
+} PortUpdateMessage;
|
|
|
|
+
|
|
|
|
+/* 新连接消息结构 */
|
|
|
|
+typedef struct {
|
|
|
|
+ MessageHeader header; // 消息头
|
|
|
|
+ char client_ip[16]; // 客户端IP
|
|
|
|
+ int client_port; // 客户端端口
|
|
|
|
+} NewConnectionMessage;
|
|
|
|
+
|
|
|
|
+/* 关闭连接消息结构 */
|
|
|
|
+typedef struct {
|
|
|
|
+ MessageHeader header; // 消息头
|
|
|
|
+} CloseConnectionMessage;
|
|
|
|
+
|
|
|
|
+/* 握手消息结构 */
|
|
|
|
+typedef struct {
|
|
|
|
+ MessageHeader header; // 消息头
|
|
|
|
+} HandshakeMessage;
|
|
|
|
+
|
|
typedef struct vmnet_queue_elem {
|
|
typedef struct vmnet_queue_elem {
|
|
- char buffer[MAX_BUFFER_SIZE];
|
|
|
|
|
|
+ char buffer[sizeof(DataMessage)];
|
|
int length;
|
|
int length;
|
|
struct vmnet_queue_elem *next;
|
|
struct vmnet_queue_elem *next;
|
|
} vmnet_queue_elem;
|
|
} vmnet_queue_elem;
|
|
@@ -172,7 +220,7 @@ void vmnet_push_data(rx_command command) {
|
|
}
|
|
}
|
|
|
|
|
|
const int size = command.sstdin_len;
|
|
const int size = command.sstdin_len;
|
|
- int approve_size = MIN(size, MAX_BUFFER_SIZE - vmnet_queue_waiting_buffer->length);
|
|
|
|
|
|
+ int approve_size = MIN(size, sizeof(DataMessage) - vmnet_queue_waiting_buffer->length);
|
|
if (approve_size > 0) {
|
|
if (approve_size > 0) {
|
|
memcpy(&vmnet_queue_waiting_buffer->buffer[vmnet_queue_waiting_buffer->length], &command.sstdin[0], approve_size);
|
|
memcpy(&vmnet_queue_waiting_buffer->buffer[vmnet_queue_waiting_buffer->length], &command.sstdin[0], approve_size);
|
|
vmnet_queue_waiting_buffer->length += approve_size;
|
|
vmnet_queue_waiting_buffer->length += approve_size;
|
|
@@ -671,56 +719,6 @@ int main() {
|
|
#include <arpa/inet.h>
|
|
#include <arpa/inet.h>
|
|
#include <pthread.h>
|
|
#include <pthread.h>
|
|
|
|
|
|
-/* 常量定义 */
|
|
|
|
-#define MAX_PORTS 16 // 最大端口数量
|
|
|
|
-#define MAX_CONNECTIONS 64 // 最大连接数
|
|
|
|
-
|
|
|
|
-/* 消息类型 */
|
|
|
|
-#define MSG_PORT_UPDATE 1 // 端口更新消息
|
|
|
|
-#define MSG_NEW_CONNECTION 2 // 新连接消息
|
|
|
|
-#define MSG_DATA 3 // 数据传输消息
|
|
|
|
-#define MSG_CLOSE_CONNECTION 4 // 关闭连接消息
|
|
|
|
-#define MSG_HANDSHAKE 5 // 握手消息
|
|
|
|
-
|
|
|
|
-/* 消息头结构 */
|
|
|
|
-typedef struct {
|
|
|
|
- int type; // 消息类型
|
|
|
|
- int connection_id; // 连接ID
|
|
|
|
- int port; // 端口号
|
|
|
|
- int data_size; // 数据大小
|
|
|
|
- int target; // 接收方标识(0=host, 1=client)
|
|
|
|
-} MessageHeader;
|
|
|
|
-
|
|
|
|
-/* 端口更新消息结构 */
|
|
|
|
-typedef struct {
|
|
|
|
- MessageHeader header; // 消息头
|
|
|
|
- int num_ports; // 端口数量
|
|
|
|
- int ports[MAX_PORTS]; // 端口列表
|
|
|
|
-} PortUpdateMessage;
|
|
|
|
-
|
|
|
|
-/* 新连接消息结构 */
|
|
|
|
-typedef struct {
|
|
|
|
- MessageHeader header; // 消息头
|
|
|
|
- char client_ip[16]; // 客户端IP
|
|
|
|
- int client_port; // 客户端端口
|
|
|
|
-} NewConnectionMessage;
|
|
|
|
-
|
|
|
|
-/* 数据传输消息结构 */
|
|
|
|
-typedef struct {
|
|
|
|
- MessageHeader header; // 消息头
|
|
|
|
- char data[MAX_BUFFER_SIZE]; // 数据
|
|
|
|
-} DataMessage;
|
|
|
|
-
|
|
|
|
-/* 关闭连接消息结构 */
|
|
|
|
-typedef struct {
|
|
|
|
- MessageHeader header; // 消息头
|
|
|
|
-} CloseConnectionMessage;
|
|
|
|
-
|
|
|
|
-/* 握手消息结构 */
|
|
|
|
-typedef struct {
|
|
|
|
- MessageHeader header; // 消息头
|
|
|
|
-} HandshakeMessage;
|
|
|
|
-
|
|
|
|
/* 函数声明 */
|
|
/* 函数声明 */
|
|
// 文件操作函数
|
|
// 文件操作函数
|
|
int open_proxy_file(int flags);
|
|
int open_proxy_file(int flags);
|
|
@@ -1307,11 +1305,11 @@ void handle_handshake_message(HandshakeMessage *message) {
|
|
* @return NULL
|
|
* @return NULL
|
|
*/
|
|
*/
|
|
void *message_handler_thread(void *arg) {
|
|
void *message_handler_thread(void *arg) {
|
|
- char buffer[MAX_BUFFER_SIZE];
|
|
|
|
|
|
+ char buffer[sizeof(DataMessage)];
|
|
|
|
|
|
while (1) {
|
|
while (1) {
|
|
// 读取消息
|
|
// 读取消息
|
|
- int bytes_read = read_message(proxy_fd, buffer, MAX_BUFFER_SIZE, 1); // 1表示client
|
|
|
|
|
|
+ int bytes_read = read_message(proxy_fd, buffer, sizeof(buffer), 1); // 1表示client
|
|
|
|
|
|
if (bytes_read <= 0) {
|
|
if (bytes_read <= 0) {
|
|
usleep(1000 * 1); // 如果没有消息,等待一段时间(1 ms)
|
|
usleep(1000 * 1); // 如果没有消息,等待一段时间(1 ms)
|