优化
This commit is contained in:
24
cap.c
24
cap.c
@@ -10,7 +10,7 @@ struct bpf_program fp; // 编译后的过滤器
|
||||
pid_t pid = -1; // 子进程全局PID
|
||||
|
||||
#define SHM_SIZE 1024 // 共享内存大小
|
||||
#define SHM_KEY 1234 // 共享内存键值
|
||||
#define SHM_KEY 0124 // 共享内存键值
|
||||
int shmid = -1;
|
||||
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
||||
char *RULE_NAME = NULL; // 共享内存
|
||||
@@ -219,6 +219,11 @@ void cleanup_(int signum)
|
||||
{
|
||||
|
||||
_printf("Received signal %d, cleaning up...\n", signum);
|
||||
|
||||
// 终止子进程
|
||||
if (pid > 0) {
|
||||
kill(pid, SIGTERM);
|
||||
}
|
||||
|
||||
// 释放共享内存
|
||||
if (RULE_NAME != NULL) {
|
||||
@@ -243,10 +248,7 @@ void cleanup_(int signum)
|
||||
pcap_freealldevs(alldevs); // 释放设备列表
|
||||
pcap_close(handle); // 关闭会话句柄
|
||||
|
||||
// 终止子进程
|
||||
if (pid > 0) {
|
||||
kill(pid, SIGTERM);
|
||||
}
|
||||
|
||||
|
||||
// 退出主进程
|
||||
exit(0);
|
||||
@@ -282,7 +284,8 @@ int main(int argc, char **argv)
|
||||
{ 0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) {
|
||||
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex)))
|
||||
{
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
if (daemon(1, 1)) {
|
||||
@@ -304,8 +307,6 @@ int main(int argc, char **argv)
|
||||
"for n in $(seq 0 %d); do iptables -A INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done",
|
||||
MAXIPSET_RULT_NAME_NUM);
|
||||
system(Ipset_Command);
|
||||
|
||||
//system("for n in $(seq 0 MAXIPSET_RULT_NAME_NUM); do iptables -A INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done");
|
||||
exit(0);
|
||||
} else if (strcmp(optarg, "stop") == 0) {
|
||||
memset(Ipset_Command, 0, BUFFER);
|
||||
@@ -314,8 +315,6 @@ int main(int argc, char **argv)
|
||||
"for n in $(seq 0 %d); do iptables -D INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done",
|
||||
MAXIPSET_RULT_NAME_NUM);
|
||||
system(Ipset_Command);
|
||||
|
||||
//system("for n in $(seq 0 MAXIPSET_RULT_NAME_NUM); do iptables -D INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done");
|
||||
exit(0);
|
||||
} else {
|
||||
usage();
|
||||
@@ -335,11 +334,6 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(interface, "eth0") != 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 创建共享内存
|
||||
shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);
|
||||
|
||||
Reference in New Issue
Block a user