Skip to content

Commit e6dfb07

Browse files
feat: Improve iptables rules management
1 parent 4c80e69 commit e6dfb07

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

src/fakehttp.c

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static int execute_command(char **argv, int silent)
291291

292292
execvp(argv[0], argv);
293293

294-
E("ERROR: execvp(): %s", strerror(errno));
294+
E("ERROR: execvp(): %s: %s", argv[0], strerror(errno));
295295

296296
_exit(EXIT_FAILURE);
297297
}
@@ -318,25 +318,38 @@ static int execute_command(char **argv, int silent)
318318
}
319319

320320

321-
static void ipt_rules_cleanup(void)
321+
static int ipt_rules_flush(int auto_create)
322322
{
323-
size_t i, ipt_cmds_cnt;
324-
char *ipt_cmds[][32] = {
325-
{"iptables", "-w", "-t", "mangle", "-F", "FAKEHTTP", NULL},
326-
327-
{"iptables", "-w", "-t", "mangle", "-D", "INPUT", "-j", "FAKEHTTP",
328-
NULL},
323+
int res;
324+
size_t i, cnt;
325+
char *ipt_flush_cmd[] = {"iptables", "-w", "-t", "mangle",
326+
"-F", "FAKEHTTP", NULL};
327+
char *ipt_create_cmds[][32] = {
328+
{"iptables", "-w", "-t", "mangle", "-N", "FAKEHTTP", NULL},
329329

330-
{"iptables", "-w", "-t", "mangle", "-D", "FORWARD", "-j", "FAKEHTTP",
330+
{"iptables", "-w", "-t", "mangle", "-I", "INPUT", "-j", "FAKEHTTP",
331331
NULL},
332332

333-
{"iptables", "-w", "-t", "mangle", "-X", "FAKEHTTP", NULL}};
333+
{"iptables", "-w", "-t", "mangle", "-I", "FORWARD", "-j", "FAKEHTTP",
334+
NULL}};
334335

335-
ipt_cmds_cnt = sizeof(ipt_cmds) / sizeof(*ipt_cmds);
336+
res = execute_command(ipt_flush_cmd, 1);
337+
if (res < 0) {
338+
if (!auto_create) {
339+
return -1;
340+
}
336341

337-
for (i = 0; i < ipt_cmds_cnt; i++) {
338-
execute_command(ipt_cmds[i], 1);
342+
cnt = sizeof(ipt_create_cmds) / sizeof(*ipt_create_cmds);
343+
for (i = 0; i < cnt; i++) {
344+
res = execute_command(ipt_create_cmds[i], 0);
345+
if (res) {
346+
E("ERROR: execute_command()");
347+
return -1;
348+
}
349+
}
339350
}
351+
352+
return 0;
340353
}
341354

342355

@@ -346,14 +359,6 @@ static int ipt_rules_setup(void)
346359
size_t i, ipt_cmds_cnt, ipt_opt_cmds_cnt;
347360
int res;
348361
char *ipt_cmds[][32] = {
349-
{"iptables", "-w", "-t", "mangle", "-N", "FAKEHTTP", NULL},
350-
351-
{"iptables", "-w", "-t", "mangle", "-I", "INPUT", "-j", "FAKEHTTP",
352-
NULL},
353-
354-
{"iptables", "-w", "-t", "mangle", "-I", "FORWARD", "-j", "FAKEHTTP",
355-
NULL},
356-
357362
/*
358363
exclude marked packets
359364
*/
@@ -1042,11 +1047,16 @@ int main(int argc, char *argv[])
10421047
/*
10431048
Iptables
10441049
*/
1045-
ipt_rules_cleanup();
1050+
res = ipt_rules_flush(1);
1051+
if (res) {
1052+
E("ERROR: ipt_rules_flush()");
1053+
goto destroy_queue;
1054+
}
1055+
10461056
res = ipt_rules_setup();
10471057
if (res) {
10481058
E("ERROR: ipt_rules_setup()");
1049-
goto cleanup_iptables;
1059+
goto flush_iptables;
10501060
}
10511061

10521062
/*
@@ -1064,7 +1074,7 @@ int main(int argc, char *argv[])
10641074
res = signal_setup();
10651075
if (res) {
10661076
E("ERROR: signal_setup()");
1067-
goto cleanup_iptables;
1077+
goto flush_iptables;
10681078
}
10691079

10701080
E("listening on %s, netfilter queue number %" PRIu32 "...", g_iface,
@@ -1077,7 +1087,7 @@ int main(int argc, char *argv[])
10771087
while (!g_exit) {
10781088
if (err_cnt >= 20) {
10791089
E("too many errors, exiting...");
1080-
goto cleanup_iptables;
1090+
goto flush_iptables;
10811091
}
10821092

10831093
recv_len = recv(fd, buff, buffsize, 0);
@@ -1094,7 +1104,7 @@ int main(int argc, char *argv[])
10941104
default:
10951105
E("ERROR: recv(): %s", strerror(errno));
10961106
err_cnt++;
1097-
goto cleanup_iptables;
1107+
goto flush_iptables;
10981108
}
10991109
}
11001110

@@ -1111,8 +1121,8 @@ int main(int argc, char *argv[])
11111121
E("exiting normally...");
11121122
exitcode = EXIT_SUCCESS;
11131123

1114-
cleanup_iptables:
1115-
ipt_rules_cleanup();
1124+
flush_iptables:
1125+
ipt_rules_flush(0);
11161126

11171127
destroy_queue:
11181128
nfq_destroy_queue(qh);

0 commit comments

Comments
 (0)