-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
145 lines (119 loc) · 3.1 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright 2017 [email protected] (Yiyuan Zhong)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <signal.h>
#include <net/if_arp.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "configure.h"
#include "httpd.h"
#include "iptables.h"
static volatile sig_atomic_t g_quit;
static void on_signal_quit(int signum)
{
(void)signum;
g_quit = 1;
}
static int initialize_signals(void)
{
struct sigaction sa;
int i;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
for (i = 1; i < NSIG; ++i) {
sigaction(i, &sa, NULL);
}
sa.sa_handler = on_signal_quit;
if (sigaction(SIGHUP , &sa, NULL) ||
sigaction(SIGINT , &sa, NULL) ||
sigaction(SIGQUIT, &sa, NULL) ||
sigaction(SIGTERM, &sa, NULL) ){
return -1;
}
sa.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &sa, NULL)) {
return -1;
}
return 0;
}
static int check_device(const char *device)
{
struct ifaddrs *ifa;
struct ifaddrs *p;
if (getifaddrs(&ifa)) {
return -1;
}
for (p = ifa; p; p = p->ifa_next) {
if (!p->ifa_addr) {
continue;
}
if (p->ifa_addr->sa_family == AF_INET &&
strcmp(p->ifa_name, device) == 0 ){
freeifaddrs(ifa);
return 0;
}
}
freeifaddrs(ifa);
return -1;
}
int main(int argc, char *argv[])
{
struct configure *c;
struct iptables *i;
struct timespec tv;
struct httpd *h;
if (argc != 2) {
fprintf(stderr, "%s <config filename>\n", argv[0]);
return EXIT_FAILURE;
}
if (initialize_signals()) {
return EXIT_FAILURE;
}
c = configure_load(argv[1]);
if (!c) {
fprintf(stderr, "Failed to parse configure file\n");
return EXIT_FAILURE;
}
if (!c->device || check_device(c->device)) {
fprintf(stderr, "Device [%s] doesn't exist\n", c->device);
configure_free(c);
return EXIT_FAILURE;
}
i = iptables_start(c);
if (!i) {
fprintf(stderr, "Failed to initialize iptables\n");
configure_free(c);
return EXIT_FAILURE;
}
h = httpd_start(c, i);
if (!h) {
fprintf(stderr, "Failed to initialize httpd\n");
iptables_stop(i);
configure_free(c);
return EXIT_FAILURE;
}
memset(&tv, 0, sizeof(tv));
tv.tv_nsec = 100000000;
while (!g_quit) {
nanosleep(&tv, NULL);
iptables_cleanup(i);
}
httpd_stop(h);
iptables_stop(i);
configure_free(c);
return EXIT_SUCCESS;
}