2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-helper-hijack-dns.c
24 * @author Philipp Tölke
28 #include "gnunet_common.h"
30 int fork_and_exec(char* file, char* cmd[]) {
33 fprintf(stderr, "could not fork: %s\n", strerror(errno));
44 return WIFEXITED(st) && (WEXITSTATUS(st) == 0);
47 int main(int argc, char** argv) {
51 if (argc < 3) return GNUNET_SYSERR;
53 if (strncmp(argv[1], "-d", 2) == 0) {
54 if (argc < 3) return GNUNET_SYSERR;
63 if (port == 0) return GNUNET_SYSERR;
66 if (stat("/sbin/iptables", &s) < 0) {
67 fprintf(stderr, "stat on /sbin/iptables failed: %s\n", strerror(errno));
70 if (stat("/sbin/ip", &s) < 0) {
71 fprintf(stderr, "stat on /sbin/ip failed: %s\n", strerror(errno));
76 snprintf(localport, 7, "%d", port);
81 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "route", "del", "default", "via", virt_dns,"table","2", NULL});
83 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "rule", "del", "fwmark", "3", "table","2", NULL});
85 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", "--dport", "53", "-j", "MARK", "--set-mark", "3", NULL});
87 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", "--sport", localport, "--dport", "53", "-j", "ACCEPT", NULL});
90 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-I", "OUTPUT", "1", "-p", "udp", "--sport", localport, "--dport", "53", "-j", "ACCEPT", NULL});
92 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p", "udp", "--dport", "53", "-j", "MARK", "--set-mark", "3", NULL});
94 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "rule", "add", "fwmark", "3", "table","2", NULL});
96 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "route", "add", "default", "via", virt_dns, "table","2", NULL});
99 if (r) return GNUNET_YES;
100 return GNUNET_SYSERR;