diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..cd3d381 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "gnu++17", + "intelliSenseMode": "linux-gcc-x64", + "configurationProvider": "ms-vscode.makefile-tools" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cap.c b/cap.c index 1e09431..0dcceee 100644 --- a/cap.c +++ b/cap.c @@ -227,6 +227,7 @@ void cleanup_(int signum) // 在程序结束时,清理缓存链表 free_ip_cache(); + if (ip2region_area != NULL) { free(ip2region_area); ip2region_area = NULL; @@ -257,22 +258,18 @@ int main(int argc, char **argv) // 注册 SIGTERM 信号处理函数 signal(SIGTERM, cleanup_); - int opt; char errbuf[PCAP_ERRBUF_SIZE]; // 错误缓冲区 - - char protocol[] = "tcp"; - char interface[256] = "{ 0 }"; - strcpy(interface, "eth0"); + char Ipset_Command[BUFFER]; + strcpy(interface, "eth0"); memset(&alldevs, 0, sizeof(alldevs)); memset(&device, 0, sizeof(device)); memset(errbuf, 0, PCAP_ERRBUF_SIZE); - char Ipset_Command[BUFFER]; - + while ((opt = getopt(argc, argv, "di:s:h?")) != -1) { switch (opt) { case 'd': @@ -329,6 +326,7 @@ int main(int argc, char **argv) perror("shmget"); exit(1); } + // 连接共享内存到进程地址空间 RULE_NAME = (char *)shmat(shmid, NULL, 0); if (RULE_NAME == (char *)-1) { @@ -336,6 +334,7 @@ int main(int argc, char **argv) exit(1); } + pid = fork(); // 创建子进程 if (pid == 0) // 子进程 { @@ -418,7 +417,6 @@ int main(int argc, char **argv) return 1; } - // 开始捕获数据包 if (pcap_loop(handle, 0, packet_handler, NULL) < 0) { fprintf(stderr, "捕获数据包时出错: %s\n", pcap_geterr(handle)); diff --git a/common.c b/common.c index 043a308..a4acd09 100644 --- a/common.c +++ b/common.c @@ -141,15 +141,17 @@ char *_time() { return temp; // 返回静态缓冲区地址 } +// 将字符串转换为IPv4地址 int is_valid_ip(const char *ip) { struct sockaddr_in sa; - // 尝试将字符串转换为IPv4地址 + // 将字符串转换为IPv4地址 int result = inet_pton(AF_INET, ip, &(sa.sin_addr)); return result != 0; } +// 修改进程优先级 int _nice(int increment) { // 获取当前优先级 int oldprio = getpriority(PRIO_PROCESS, getpid());