-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiptables_wrapper.c
74 lines (70 loc) · 2.41 KB
/
iptables_wrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "iptables_wrapper.h"
void init_iptables(Argument *arg) {
argument = arg;
char *rule = malloc(strlen(INIT_IPTABLES_TEMPLE) + DEFAULT_FILL_LEN);
if (argument->protocol == UDP) {
sprintf(rule, INIT_IPTABLES_TEMPLE, argument->interface_name, "udp", argument->protect_port);
} else {//TCP
sprintf(rule, INIT_IPTABLES_TEMPLE, argument->interface_name, "tcp", argument->protect_port);
}
if (verbose) {
printf("Initializing iptables rule...\n");
printf(rule);
printf("\n");
}
if (system(rule)) {
printf("Error initializing iptables rule\n");
}
free(rule);
}
void clean_iptables() {
char *rule = malloc(strlen(CLEAN_IPTABLES_TEMPLE) + DEFAULT_FILL_LEN);
if (argument->protocol == UDP) {
sprintf(rule, CLEAN_IPTABLES_TEMPLE, argument->interface_name, "udp", argument->protect_port);
} else {//TCP
sprintf(rule, CLEAN_IPTABLES_TEMPLE, argument->interface_name, "tcp", argument->protect_port);
}
if (verbose) {
printf("Cleaning iptables rule...\n");
printf(rule);
printf("\n");
}
if (system(rule)) {
printf("Error cleaning iptables rule!\n");
}
free(rule);
}
void insert_client(char *addr) {
char *rule = malloc(strlen(INSERT_IPTABLES_TEMPLE) + DEFAULT_FILL_LEN);
if (argument->protocol == UDP) {
sprintf(rule, INSERT_IPTABLES_TEMPLE, argument->interface_name, "udp", argument->protect_port, addr);
} else {//TCP
sprintf(rule, INSERT_IPTABLES_TEMPLE, argument->interface_name, "tcp", argument->protect_port, addr);
}
if (verbose) {
printf("Inserting iptables rule...\n");
printf(rule);
printf("\n");
}
if (system(rule)) {
printf("Error adding iptables rule for %s\n", addr);
}
free(rule);
}
void remove_client(char *addr) {
char *rule = malloc(strlen(REMOVE_IPTABLES_TEMPLE) + DEFAULT_FILL_LEN);
if (argument->protocol == UDP) {
sprintf(rule, REMOVE_IPTABLES_TEMPLE, argument->interface_name, "udp", argument->protect_port, addr);
} else {//TCP
sprintf(rule, REMOVE_IPTABLES_TEMPLE, argument->interface_name, "tcp", argument->protect_port, addr);
}
if (verbose) {
printf("Removing iptables rule...\n");
printf(rule);
printf("\n");
}
if (system(rule)) {
printf("Error removing iptables rule for %s\n", addr);
}
free(rule);
}