|
@@ -22,6 +22,8 @@
|
|
|
#define VMP_TX_POOL_SIZE 64
|
|
|
#define VMP_STRBUF_LEN 256
|
|
|
|
|
|
+extern int vmnetproxy_main(void);
|
|
|
+
|
|
|
typedef struct {
|
|
|
short width;
|
|
|
short height;
|
|
@@ -490,6 +492,12 @@ void rx_process(rx_command cmd) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void *vmnetproxy_program(void *param) {
|
|
|
+ int vret = vmnetproxy_main();
|
|
|
+ printf("XCVMKit-OS: vnmetproxy killed with %d", vret);
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
int main() {
|
|
|
printf("Boot Success\n");
|
|
|
printf("Welcome to XCVMKit-OS!\n");
|
|
@@ -517,6 +525,9 @@ int main() {
|
|
|
bzero(&initial_cmd.sstdout, VMP_STRBUF_LEN);
|
|
|
tx_push(initial_cmd);
|
|
|
|
|
|
+ pthread_t vmnetproxy_thr;
|
|
|
+ pthread_create(&vmnetproxy_thr, NULL, vmnetproxy_program, NULL);
|
|
|
+
|
|
|
while (1) {
|
|
|
rx_command command;
|
|
|
size_t read_size = read(RX, &command, sizeof(rx_command));
|
|
@@ -527,3 +538,946 @@ int main() {
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
+/// vmnetproxy code
|
|
|
+#ifndef COMMON_H
|
|
|
+#define COMMON_H
|
|
|
+
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <string.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <fcntl.h>
|
|
|
+#include <errno.h>
|
|
|
+#include <sys/types.h>
|
|
|
+#include <sys/socket.h>
|
|
|
+#include <sys/select.h>
|
|
|
+#include <netinet/in.h>
|
|
|
+#include <arpa/inet.h>
|
|
|
+#include <pthread.h>
|
|
|
+
|
|
|
+/* 常量定义 */
|
|
|
+#define PROXY_FILE "/etc/.vmpdrvfna" // 共享文件路径
|
|
|
+#define MAX_BUFFER_SIZE 4096 // 最大缓冲区大小
|
|
|
+#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);
|
|
|
+void lock_file(int fd);
|
|
|
+void unlock_file(int fd);
|
|
|
+
|
|
|
+// 消息处理函数
|
|
|
+int write_message(int fd, void *message, int size);
|
|
|
+int read_message(int fd, void *buffer, int size, int target);
|
|
|
+int clear_message_queue(int fd);
|
|
|
+int send_handshake_message(int fd, int target);
|
|
|
+
|
|
|
+// 网络操作函数
|
|
|
+int create_server_socket(int port);
|
|
|
+int accept_connection(int server_socket);
|
|
|
+int connect_to_server(const char *ip, int port);
|
|
|
+
|
|
|
+// 端口扫描函数
|
|
|
+int get_listening_ports(int *ports, int max_ports);
|
|
|
+
|
|
|
+// 日志函数
|
|
|
+void log_message(const char *format, ...);
|
|
|
+
|
|
|
+#endif /* COMMON_H */
|
|
|
+
|
|
|
+#include <stdarg.h>
|
|
|
+#include <time.h>
|
|
|
+#include <sys/file.h>
|
|
|
+#include <netdb.h>
|
|
|
+#include <ifaddrs.h>
|
|
|
+#include <sys/ioctl.h>
|
|
|
+
|
|
|
+/* 文件操作函数 */
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打开代理文件
|
|
|
+ * @param flags 打开文件的标志
|
|
|
+ * @return 文件描述符
|
|
|
+ */
|
|
|
+int open_proxy_file(int flags) {
|
|
|
+ int fd = open(PROXY_FILE, flags, 1101824);
|
|
|
+ if (fd < 0) {
|
|
|
+ log_message("无法打开代理文件: %s", strerror(errno));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return fd;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 锁定文件以进行独占访问
|
|
|
+ * @param fd 文件描述符
|
|
|
+ */
|
|
|
+void lock_file(int fd) {
|
|
|
+ if (flock(fd, LOCK_EX) < 0) {
|
|
|
+ log_message("无法锁定文件: %s", strerror(errno));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 解锁文件
|
|
|
+ * @param fd 文件描述符
|
|
|
+ */
|
|
|
+void unlock_file(int fd) {
|
|
|
+ if (flock(fd, LOCK_UN) < 0) {
|
|
|
+ log_message("无法解锁文件: %s", strerror(errno));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* 消息处理函数 */
|
|
|
+
|
|
|
+/**
|
|
|
+ * 写入消息到文件
|
|
|
+ * @param fd 文件描述符
|
|
|
+ * @param message 消息指针
|
|
|
+ * @param size 消息大小
|
|
|
+ * @return 写入的字节数
|
|
|
+ */
|
|
|
+int write_message(int fd, void *message, int size) {
|
|
|
+ lock_file(fd);
|
|
|
+
|
|
|
+ // 将文件指针移动到文件末尾(追加写入)
|
|
|
+ if (lseek(fd, 0, SEEK_END) < 0) {
|
|
|
+ log_message("无法定位文件指针到末尾: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入消息大小(用于读取时分隔消息)
|
|
|
+ int total_size = size + sizeof(int);
|
|
|
+ if (write(fd, &total_size, sizeof(int)) < 0) {
|
|
|
+ log_message("写入消息大小失败: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入消息内容
|
|
|
+ int bytes_written = write(fd, message, size);
|
|
|
+ if (bytes_written < 0) {
|
|
|
+ log_message("写入消息失败: %s", strerror(errno));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 刷新文件缓冲区
|
|
|
+ fsync(fd);
|
|
|
+
|
|
|
+ unlock_file(fd);
|
|
|
+ return bytes_written;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 从文件读取消息
|
|
|
+ * @param fd 文件描述符
|
|
|
+ * @param buffer 缓冲区指针
|
|
|
+ * @param size 缓冲区大小
|
|
|
+ * @param target 目标接收方(0=host, 1=client)
|
|
|
+ * @return 读取的字节数,如果没有消息则返回0
|
|
|
+ */
|
|
|
+int read_message(int fd, void *buffer, int size, int target) {
|
|
|
+ lock_file(fd);
|
|
|
+
|
|
|
+ // 将文件指针移动到文件开头
|
|
|
+ if (lseek(fd, 0, SEEK_SET) < 0) {
|
|
|
+ log_message("无法定位文件指针: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取文件大小
|
|
|
+ off_t file_size = lseek(fd, 0, SEEK_END);
|
|
|
+ if (file_size < 0) {
|
|
|
+ log_message("无法获取文件大小: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果文件为空,则没有消息
|
|
|
+ if (file_size == 0) {
|
|
|
+ unlock_file(fd);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重新定位到文件开头
|
|
|
+ if (lseek(fd, 0, SEEK_SET) < 0) {
|
|
|
+ log_message("无法重新定位文件指针: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ int message_size;
|
|
|
+ int content_size;
|
|
|
+ long total_message_size = 0;
|
|
|
+ while (1)
|
|
|
+ {
|
|
|
+ // 读取第一条消息的大小
|
|
|
+ if (read(fd, &message_size, sizeof(int)) != sizeof(int)) {
|
|
|
+ log_message("读取消息大小失败: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ total_message_size += message_size;
|
|
|
+
|
|
|
+ // 检查消息大小是否合理
|
|
|
+ content_size = message_size - sizeof(int);
|
|
|
+ if (content_size <= 0 || content_size > size) {
|
|
|
+ log_message("消息大小不合理: %d", content_size);
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 先读取消息头部以检查target
|
|
|
+ MessageHeader header;
|
|
|
+ int header_size = sizeof(MessageHeader) < content_size ? sizeof(MessageHeader) : content_size;
|
|
|
+ int peek_bytes = read(fd, &header, header_size);
|
|
|
+ if (peek_bytes < 0) {
|
|
|
+ log_message("读取消息头部失败: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将文件指针移回消息内容开始位置
|
|
|
+ if (lseek(fd, -peek_bytes, SEEK_CUR) < 0) {
|
|
|
+ log_message("无法重新定位文件指针: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查消息target是否匹配
|
|
|
+ if (header.target == target) {
|
|
|
+ // 是发给当前接收方的消息,跳过当前消息,继续查找后续消息
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // 不是发给当前接收方的消息,跳过当前消息,继续查找后续消息
|
|
|
+
|
|
|
+ // 跳过当前消息内容
|
|
|
+ if (lseek(fd, content_size, SEEK_CUR) < 0) {
|
|
|
+ log_message("无法跳过当前消息: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算剩余数据大小
|
|
|
+ off_t current_position = lseek(fd, 0, SEEK_CUR);
|
|
|
+ if (current_position < 0) {
|
|
|
+ log_message("无法获取当前位置: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果还有剩余数据,继续查找
|
|
|
+ if (current_position < file_size) {
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ // 没有更多消息了
|
|
|
+ unlock_file(fd);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取完整消息内容
|
|
|
+ int bytes_read = read(fd, buffer, content_size);
|
|
|
+ if (bytes_read < 0) {
|
|
|
+ log_message("读取消息内容失败: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算剩余数据大小
|
|
|
+ off_t remaining_size = file_size - total_message_size;
|
|
|
+ off_t before_size = total_message_size - content_size - sizeof(int);
|
|
|
+
|
|
|
+ // 如果还有剩余数据,将其移动到文件开头
|
|
|
+ if (remaining_size > 0) {
|
|
|
+ char *temp_buffer = malloc(remaining_size);
|
|
|
+ if (temp_buffer == NULL) {
|
|
|
+ log_message("内存分配失败");
|
|
|
+ unlock_file(fd);
|
|
|
+ return bytes_read;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取剩余数据
|
|
|
+ if (read(fd, temp_buffer, remaining_size) != remaining_size) {
|
|
|
+ log_message("读取剩余数据失败: %s", strerror(errno));
|
|
|
+ free(temp_buffer);
|
|
|
+ unlock_file(fd);
|
|
|
+ return bytes_read;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将文件截断为0
|
|
|
+ if (ftruncate(fd, before_size) < 0) {
|
|
|
+ log_message("截断文件失败: %s", strerror(errno));
|
|
|
+ free(temp_buffer);
|
|
|
+ unlock_file(fd);
|
|
|
+ return bytes_read;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将文件指针移回开头
|
|
|
+ if (lseek(fd, before_size, SEEK_SET) < 0) {
|
|
|
+ log_message("无法重新定位文件指针: %s", strerror(errno));
|
|
|
+ free(temp_buffer);
|
|
|
+ unlock_file(fd);
|
|
|
+ return bytes_read;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写回剩余数据
|
|
|
+ if (write(fd, temp_buffer, remaining_size) != remaining_size) {
|
|
|
+ log_message("写回剩余数据失败: %s", strerror(errno));
|
|
|
+ }
|
|
|
+
|
|
|
+ free(temp_buffer);
|
|
|
+ } else {
|
|
|
+ // 如果没有剩余数据,将文件截断为0
|
|
|
+ if (ftruncate(fd, before_size) < 0) {
|
|
|
+ log_message("截断文件失败: %s", strerror(errno));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ unlock_file(fd);
|
|
|
+ return bytes_read;
|
|
|
+}
|
|
|
+
|
|
|
+/* 网络操作函数 */
|
|
|
+
|
|
|
+/**
|
|
|
+ * 创建服务器套接字
|
|
|
+ * @param port 端口号
|
|
|
+ * @return 套接字描述符
|
|
|
+ */
|
|
|
+int create_server_socket(int port) {
|
|
|
+ int server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ if (server_socket < 0) {
|
|
|
+ log_message("创建套接字失败: %s", strerror(errno));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置套接字选项,允许地址重用
|
|
|
+ int opt = 1;
|
|
|
+ if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
|
|
+ log_message("设置套接字选项失败: %s", strerror(errno));
|
|
|
+ close(server_socket);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 绑定地址
|
|
|
+ struct sockaddr_in server_addr;
|
|
|
+ memset(&server_addr, 0, sizeof(server_addr));
|
|
|
+ server_addr.sin_family = AF_INET;
|
|
|
+ server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
+ server_addr.sin_port = htons(port);
|
|
|
+
|
|
|
+ if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
|
+ log_message("绑定套接字失败: %s", strerror(errno));
|
|
|
+ close(server_socket);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 监听连接
|
|
|
+ if (listen(server_socket, 5) < 0) {
|
|
|
+ log_message("监听套接字失败: %s", strerror(errno));
|
|
|
+ close(server_socket);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return server_socket;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 接受连接
|
|
|
+ * @param server_socket 服务器套接字
|
|
|
+ * @return 客户端套接字
|
|
|
+ */
|
|
|
+int accept_connection(int server_socket) {
|
|
|
+ struct sockaddr_in client_addr;
|
|
|
+ socklen_t client_len = sizeof(client_addr);
|
|
|
+
|
|
|
+ int client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
|
|
|
+ if (client_socket < 0) {
|
|
|
+ log_message("接受连接失败: %s", strerror(errno));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ log_message("接受来自 %s:%d 的连接", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
|
|
+ return client_socket;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 连接到服务器
|
|
|
+ * @param ip 服务器IP
|
|
|
+ * @param port 服务器端口
|
|
|
+ * @return 套接字描述符
|
|
|
+ */
|
|
|
+int connect_to_server(const char *ip, int port) {
|
|
|
+ int client_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ if (client_socket < 0) {
|
|
|
+ log_message("创建套接字失败: %s", strerror(errno));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ struct sockaddr_in server_addr;
|
|
|
+ memset(&server_addr, 0, sizeof(server_addr));
|
|
|
+ server_addr.sin_family = AF_INET;
|
|
|
+ server_addr.sin_port = htons(port);
|
|
|
+
|
|
|
+ if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
|
|
|
+ log_message("无效的IP地址: %s", ip);
|
|
|
+ close(client_socket);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
|
+ log_message("连接到服务器失败: %s", strerror(errno));
|
|
|
+ close(client_socket);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return client_socket;
|
|
|
+}
|
|
|
+
|
|
|
+/* 端口扫描函数 */
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取本地监听的端口列表
|
|
|
+ * @param ports 端口数组
|
|
|
+ * @param max_ports 最大端口数量
|
|
|
+ * @return 找到的端口数量
|
|
|
+ */
|
|
|
+int get_listening_ports(int *ports, int max_ports) {
|
|
|
+ FILE *fp;
|
|
|
+ char line[256];
|
|
|
+ int count = 0;
|
|
|
+
|
|
|
+ // 使用netstat命令获取监听的TCP端口
|
|
|
+ fp = popen("netstat -tln | grep LISTEN", "r");
|
|
|
+ if (fp == NULL) {
|
|
|
+ log_message("执行netstat命令失败: %s", strerror(errno));
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (fgets(line, sizeof(line), fp) != NULL && count < max_ports) {
|
|
|
+ // 检查行是否包含 "LISTEN" 关键字,过滤掉错误信息
|
|
|
+ if (strstr(line, "LISTEN") == NULL) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ char *ptr = strstr(line, ":");
|
|
|
+ if (ptr) {
|
|
|
+ int port = atoi(ptr + 1);
|
|
|
+ if (port > 0) {
|
|
|
+ // 检查端口是否已经在列表中
|
|
|
+ int i;
|
|
|
+ for (i = 0; i < count; i++) {
|
|
|
+ if (ports[i] == port) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果端口不在列表中,添加它
|
|
|
+ if (i == count) {
|
|
|
+ ports[count++] = port;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pclose(fp);
|
|
|
+ return count;
|
|
|
+}
|
|
|
+
|
|
|
+/* 日志函数 */
|
|
|
+
|
|
|
+/**
|
|
|
+ * 记录日志消息
|
|
|
+ * @param format 格式字符串
|
|
|
+ * @param ... 可变参数
|
|
|
+ */
|
|
|
+void log_message(const char *format, ...) {
|
|
|
+ va_list args;
|
|
|
+ char buffer[1024];
|
|
|
+ time_t now;
|
|
|
+ struct tm *timeinfo;
|
|
|
+
|
|
|
+ // 获取当前时间
|
|
|
+ time(&now);
|
|
|
+ timeinfo = localtime(&now);
|
|
|
+
|
|
|
+ // 格式化时间
|
|
|
+ char time_str[20];
|
|
|
+ strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", timeinfo);
|
|
|
+
|
|
|
+ // 格式化日志消息
|
|
|
+ va_start(args, format);
|
|
|
+ vsnprintf(buffer, sizeof(buffer), format, args);
|
|
|
+ va_end(args);
|
|
|
+
|
|
|
+ // 输出日志
|
|
|
+ printf("[%s] %s\n", time_str, buffer);
|
|
|
+ fflush(stdout);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 清空消息队列
|
|
|
+ * @param fd 文件描述符
|
|
|
+ * @return 0表示成功,-1表示失败
|
|
|
+ */
|
|
|
+int clear_message_queue(int fd) {
|
|
|
+ lock_file(fd);
|
|
|
+
|
|
|
+ // 将文件截断为0字节,清空所有内容
|
|
|
+ if (ftruncate(fd, 0) < 0) {
|
|
|
+ log_message("清空消息队列失败: %s", strerror(errno));
|
|
|
+ unlock_file(fd);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 刷新文件缓冲区
|
|
|
+ fsync(fd);
|
|
|
+
|
|
|
+ unlock_file(fd);
|
|
|
+ log_message("消息队列已清空");
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 发送握手消息
|
|
|
+ * @param fd 文件描述符
|
|
|
+ * @param target 目标接收方(0=host, 1=client)
|
|
|
+ * @return 写入的字节数
|
|
|
+ */
|
|
|
+int send_handshake_message(int fd, int target) {
|
|
|
+ HandshakeMessage message;
|
|
|
+ memset(&message, 0, sizeof(message));
|
|
|
+ message.header.type = MSG_HANDSHAKE;
|
|
|
+ message.header.target = target;
|
|
|
+
|
|
|
+ int result = write_message(fd, &message, sizeof(message));
|
|
|
+ if (result > 0) {
|
|
|
+ log_message("已发送握手消息到%s", target == 0 ? "host" : "client");
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+/* 全局变量 */
|
|
|
+int proxy_fd = -1; // 代理文件描述符
|
|
|
+int ports[MAX_PORTS]; // 监听的端口列表
|
|
|
+int num_ports = 0; // 端口数量
|
|
|
+int connection_map[MAX_CONNECTIONS]; // 连接映射表
|
|
|
+int next_connection_id = 1; // 下一个连接ID
|
|
|
+
|
|
|
+/* 线程函数 */
|
|
|
+void *port_monitor_thread(void *arg);
|
|
|
+void *connection_handler_thread(void *arg);
|
|
|
+
|
|
|
+/**
|
|
|
+ * 初始化连接映射表
|
|
|
+ */
|
|
|
+void init_connection_map() {
|
|
|
+ for (int i = 0; i < MAX_CONNECTIONS; i++) {
|
|
|
+ connection_map[i] = -1;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 分配新的连接ID
|
|
|
+ * @return 连接ID
|
|
|
+ */
|
|
|
+int allocate_connection_id() {
|
|
|
+ for (int i = 0; i < MAX_CONNECTIONS; i++) {
|
|
|
+ if (connection_map[i] == -1) {
|
|
|
+ connection_map[i] = 0; // 标记为已分配但未连接
|
|
|
+ return i + 1; // 连接ID从1开始
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1; // 没有可用的连接ID
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 释放连接ID
|
|
|
+ * @param connection_id 连接ID
|
|
|
+ */
|
|
|
+void free_connection_id(int connection_id) {
|
|
|
+ if (connection_id > 0 && connection_id <= MAX_CONNECTIONS) {
|
|
|
+ int socket_fd = connection_map[connection_id - 1];
|
|
|
+ if (socket_fd > 0) {
|
|
|
+ close(socket_fd);
|
|
|
+ }
|
|
|
+ connection_map[connection_id - 1] = -1;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设置连接套接字
|
|
|
+ * @param connection_id 连接ID
|
|
|
+ * @param socket_fd 套接字描述符
|
|
|
+ */
|
|
|
+void set_connection_socket(int connection_id, int socket_fd) {
|
|
|
+ if (connection_id > 0 && connection_id <= MAX_CONNECTIONS) {
|
|
|
+ connection_map[connection_id - 1] = socket_fd;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取连接套接字
|
|
|
+ * @param connection_id 连接ID
|
|
|
+ * @return 套接字描述符
|
|
|
+ */
|
|
|
+int get_connection_socket(int connection_id) {
|
|
|
+ if (connection_id > 0 && connection_id <= MAX_CONNECTIONS) {
|
|
|
+ return connection_map[connection_id - 1];
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 更新端口列表
|
|
|
+ */
|
|
|
+void update_ports() {
|
|
|
+ int new_ports[MAX_PORTS];
|
|
|
+ int new_num_ports = get_listening_ports(new_ports, MAX_PORTS);
|
|
|
+
|
|
|
+ // 检查端口列表是否有变化
|
|
|
+ int changed = 0;
|
|
|
+ if (new_num_ports != num_ports) {
|
|
|
+ changed = 1;
|
|
|
+ } else {
|
|
|
+ for (int i = 0; i < num_ports; i++) {
|
|
|
+ int found = 0;
|
|
|
+ for (int j = 0; j < new_num_ports; j++) {
|
|
|
+ if (ports[i] == new_ports[j]) {
|
|
|
+ found = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!found) {
|
|
|
+ changed = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果端口列表有变化,更新并通知host
|
|
|
+ if (changed) {
|
|
|
+ log_message("端口列表已更新,共 %d 个端口", new_num_ports);
|
|
|
+
|
|
|
+ // 更新本地端口列表
|
|
|
+ num_ports = new_num_ports;
|
|
|
+ for (int i = 0; i < num_ports; i++) {
|
|
|
+ ports[i] = new_ports[i];
|
|
|
+ log_message("监听端口: %d", ports[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送端口更新消息给host
|
|
|
+ PortUpdateMessage message;
|
|
|
+ memset(&message, 0, sizeof(message));
|
|
|
+ message.header.type = MSG_PORT_UPDATE;
|
|
|
+ message.header.target = 0; // 设置接收方为host
|
|
|
+ message.num_ports = num_ports;
|
|
|
+ for (int i = 0; i < num_ports; i++) {
|
|
|
+ message.ports[i] = ports[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ write_message(proxy_fd, &message, sizeof(message));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 处理新连接消息
|
|
|
+ * @param message 新连接消息
|
|
|
+ */
|
|
|
+void handle_new_connection(NewConnectionMessage *message) {
|
|
|
+ int connection_id = message->header.connection_id;
|
|
|
+ int port = message->header.port;
|
|
|
+
|
|
|
+ log_message("收到新连接请求: ID=%d, 端口=%d, 客户端=%s:%d",
|
|
|
+ connection_id, port, message->client_ip, message->client_port);
|
|
|
+
|
|
|
+ // 连接到本地服务
|
|
|
+ int socket_fd = connect_to_server("127.0.0.1", port);
|
|
|
+ if (socket_fd < 0) {
|
|
|
+ log_message("无法连接到本地服务: 端口=%d", port);
|
|
|
+
|
|
|
+ // 发送关闭连接消息
|
|
|
+ CloseConnectionMessage close_message;
|
|
|
+ memset(&close_message, 0, sizeof(close_message));
|
|
|
+ close_message.header.type = MSG_CLOSE_CONNECTION;
|
|
|
+ close_message.header.connection_id = connection_id;
|
|
|
+ close_message.header.target = 0; // 设置接收方为host
|
|
|
+ write_message(proxy_fd, &close_message, sizeof(close_message));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置连接映射
|
|
|
+ set_connection_socket(connection_id, socket_fd);
|
|
|
+
|
|
|
+ // 创建连接处理线程
|
|
|
+ pthread_t thread;
|
|
|
+ int *thread_arg = malloc(sizeof(int));
|
|
|
+ *thread_arg = connection_id;
|
|
|
+ if (pthread_create(&thread, NULL, connection_handler_thread, thread_arg) != 0) {
|
|
|
+ log_message("创建连接处理线程失败: %s", strerror(errno));
|
|
|
+ free_connection_id(connection_id);
|
|
|
+ free(thread_arg);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ pthread_detach(thread);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 处理数据消息
|
|
|
+ * @param message 数据消息
|
|
|
+ */
|
|
|
+void handle_data_message(DataMessage *message) {
|
|
|
+ int connection_id = message->header.connection_id;
|
|
|
+ int socket_fd = get_connection_socket(connection_id);
|
|
|
+
|
|
|
+ if (socket_fd < 0) {
|
|
|
+ log_message("无效的连接ID: %d", connection_id);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将数据发送到本地服务
|
|
|
+ int bytes_sent = write(socket_fd, message->data, message->header.data_size);
|
|
|
+ if (bytes_sent < 0) {
|
|
|
+ log_message("发送数据到本地服务失败: %s", strerror(errno));
|
|
|
+
|
|
|
+ // 发送关闭连接消息
|
|
|
+ CloseConnectionMessage close_message;
|
|
|
+ memset(&close_message, 0, sizeof(close_message));
|
|
|
+ close_message.header.type = MSG_CLOSE_CONNECTION;
|
|
|
+ close_message.header.connection_id = connection_id;
|
|
|
+ close_message.header.target = 0; // 设置接收方为host
|
|
|
+ write_message(proxy_fd, &close_message, sizeof(close_message));
|
|
|
+
|
|
|
+ free_connection_id(connection_id);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 处理关闭连接消息
|
|
|
+ * @param message 关闭连接消息
|
|
|
+ */
|
|
|
+void handle_close_connection(CloseConnectionMessage *message) {
|
|
|
+ int connection_id = message->header.connection_id;
|
|
|
+ log_message("关闭连接: ID=%d", connection_id);
|
|
|
+ free_connection_id(connection_id);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 端口监控线程函数
|
|
|
+ * @param arg 线程参数
|
|
|
+ * @return NULL
|
|
|
+ */
|
|
|
+void *port_monitor_thread(void *arg) {
|
|
|
+ while (1) {
|
|
|
+ update_ports();
|
|
|
+ usleep(5000000); // 每5秒检查一次端口变化(5000000微秒 = 5秒)
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 连接处理线程函数
|
|
|
+ * @param arg 线程参数(连接ID)
|
|
|
+ * @return NULL
|
|
|
+ */
|
|
|
+void *connection_handler_thread(void *arg) {
|
|
|
+ int connection_id = *((int *)arg);
|
|
|
+ free(arg);
|
|
|
+
|
|
|
+ int socket_fd = get_connection_socket(connection_id);
|
|
|
+ if (socket_fd < 0) {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ char buffer[MAX_BUFFER_SIZE];
|
|
|
+ while (1) {
|
|
|
+ // 从本地服务读取数据
|
|
|
+ int bytes_read = read(socket_fd, buffer, MAX_BUFFER_SIZE);
|
|
|
+ if (bytes_read <= 0) {
|
|
|
+ if (bytes_read < 0) {
|
|
|
+ log_message("从本地服务读取数据失败: %s", strerror(errno));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送数据消息给host
|
|
|
+ DataMessage message;
|
|
|
+ memset(&message, 0, sizeof(message));
|
|
|
+ message.header.type = MSG_DATA;
|
|
|
+ message.header.connection_id = connection_id;
|
|
|
+ message.header.data_size = bytes_read;
|
|
|
+ message.header.target = 0; // 设置接收方为host
|
|
|
+ memcpy(message.data, buffer, bytes_read);
|
|
|
+
|
|
|
+ write_message(proxy_fd, &message, sizeof(MessageHeader) + bytes_read);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送关闭连接消息
|
|
|
+ CloseConnectionMessage close_message;
|
|
|
+ memset(&close_message, 0, sizeof(close_message));
|
|
|
+ close_message.header.type = MSG_CLOSE_CONNECTION;
|
|
|
+ close_message.header.connection_id = connection_id;
|
|
|
+ write_message(proxy_fd, &close_message, sizeof(close_message));
|
|
|
+
|
|
|
+ free_connection_id(connection_id);
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 处理握手消息
|
|
|
+ * @param message 握手消息
|
|
|
+ */
|
|
|
+void handle_handshake_message(HandshakeMessage *message) {
|
|
|
+ log_message("收到来自host的握手消息,host已上线");
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 消息处理线程函数
|
|
|
+ * @param arg 线程参数
|
|
|
+ * @return NULL
|
|
|
+ */
|
|
|
+void *message_handler_thread(void *arg) {
|
|
|
+ char buffer[MAX_BUFFER_SIZE];
|
|
|
+
|
|
|
+ while (1) {
|
|
|
+ // 读取消息
|
|
|
+ int bytes_read = read_message(proxy_fd, buffer, MAX_BUFFER_SIZE, 1); // 1表示client
|
|
|
+ if (bytes_read <= 0) {
|
|
|
+ usleep(100); // 如果没有消息,等待一段时间(100微秒)
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析消息头
|
|
|
+ MessageHeader *header = (MessageHeader *)buffer;
|
|
|
+
|
|
|
+ // 根据消息类型处理
|
|
|
+ switch (header->type) {
|
|
|
+ case MSG_NEW_CONNECTION:
|
|
|
+ handle_new_connection((NewConnectionMessage *)buffer);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case MSG_DATA:
|
|
|
+ handle_data_message((DataMessage *)buffer);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case MSG_CLOSE_CONNECTION:
|
|
|
+ handle_close_connection((CloseConnectionMessage *)buffer);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case MSG_HANDSHAKE:
|
|
|
+ handle_handshake_message((HandshakeMessage *)buffer);
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ log_message("未知的消息类型: %d", header->type);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 主函数
|
|
|
+ * @return 退出码
|
|
|
+ */
|
|
|
+int vmnetproxy_main() {
|
|
|
+ log_message("VMNet Proxy Client 启动");
|
|
|
+
|
|
|
+ // 初始化连接映射表
|
|
|
+ init_connection_map();
|
|
|
+
|
|
|
+ // 打开代理文件
|
|
|
+ proxy_fd = open_proxy_file(O_RDWR | O_CREAT);
|
|
|
+ if (proxy_fd < 0) {
|
|
|
+ log_message("无法打开代理文件,退出");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清空消息队列
|
|
|
+ if (clear_message_queue(proxy_fd) < 0) {
|
|
|
+ log_message("清空消息队列失败,继续执行");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送握手消息给host
|
|
|
+ if (send_handshake_message(proxy_fd, 0) < 0) {
|
|
|
+ log_message("发送握手消息失败,继续执行");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建端口监控线程
|
|
|
+ pthread_t port_thread;
|
|
|
+ if (pthread_create(&port_thread, NULL, port_monitor_thread, NULL) != 0) {
|
|
|
+ log_message("创建端口监控线程失败: %s", strerror(errno));
|
|
|
+ close(proxy_fd);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建消息处理线程
|
|
|
+ pthread_t message_thread;
|
|
|
+ if (pthread_create(&message_thread, NULL, message_handler_thread, NULL) != 0) {
|
|
|
+ log_message("创建消息处理线程失败: %s", strerror(errno));
|
|
|
+ close(proxy_fd);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待线程结束(实际上不会结束)
|
|
|
+ pthread_join(port_thread, NULL);
|
|
|
+ pthread_join(message_thread, NULL);
|
|
|
+
|
|
|
+ close(proxy_fd);
|
|
|
+ return 0;
|
|
|
+}
|