4 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
5 * Copyright (C) 2013 BMW Car IT GmbH.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "../src/connman.h"
31 enum iptables_command {
32 IPTABLES_COMMAND_APPEND,
33 IPTABLES_COMMAND_DELETE,
34 IPTABLES_COMMAND_POLICY,
35 IPTABLES_COMMAND_CHAIN_INSERT,
36 IPTABLES_COMMAND_CHAIN_DELETE,
37 IPTABLES_COMMAND_CHAIN_FLUSH,
38 IPTABLES_COMMAND_DUMP,
39 IPTABLES_COMMAND_UNKNOWN,
42 int main(int argc, char *argv[])
44 enum iptables_command cmd = IPTABLES_COMMAND_UNKNOWN;
45 char *table = NULL, *chain = NULL, *rule = NULL, *tmp;
50 while ((c = getopt_long(argc, argv,
51 "-A:D:P:N:X:F:Lt:", NULL, NULL)) != -1) {
55 cmd = IPTABLES_COMMAND_APPEND;
59 cmd = IPTABLES_COMMAND_DELETE;
63 /* The policy will be stored in rule. */
64 cmd = IPTABLES_COMMAND_POLICY;
68 cmd = IPTABLES_COMMAND_CHAIN_INSERT;
72 cmd = IPTABLES_COMMAND_CHAIN_DELETE;
76 cmd = IPTABLES_COMMAND_CHAIN_FLUSH;
79 cmd = IPTABLES_COMMAND_DUMP;
93 for (i = optind - 1; i < argc; i++) {
96 rule = g_strdup_printf("%s %s", rule, argv[i]);
99 rule = g_strdup(argv[i]);
102 __connman_iptables_init();
105 case IPTABLES_COMMAND_APPEND:
106 err = __connman_iptables_append(table, chain, rule);
108 case IPTABLES_COMMAND_DELETE:
109 err = __connman_iptables_delete(table, chain, rule);
111 case IPTABLES_COMMAND_POLICY:
112 err = __connman_iptables_change_policy(table, chain, rule);
114 case IPTABLES_COMMAND_CHAIN_INSERT:
115 err = __connman_iptables_new_chain(table, chain);
117 case IPTABLES_COMMAND_CHAIN_DELETE:
118 err = __connman_iptables_delete_chain(table, chain);
120 case IPTABLES_COMMAND_CHAIN_FLUSH:
121 err = __connman_iptables_flush_chain(table, chain);
123 case IPTABLES_COMMAND_DUMP:
124 __connman_log_init(argv[0], "*", FALSE, FALSE,
125 "iptables-test", "1");
126 err = __connman_iptables_dump(table);
128 case IPTABLES_COMMAND_UNKNOWN:
129 printf("Missing command\n");
130 printf("usage: iptables-test [-t table] {-A|-D} chain rule\n");
131 printf(" iptables-test [-t table] {-N|-X|-F} chain\n");
132 printf(" iptables-test [-t table] -L\n");
133 printf(" iptables-test [-t table] -P chain target\n");
138 printf("Error: %s\n", strerror(-err));
142 err = __connman_iptables_commit(table);
144 printf("Failed to commit changes: %s\n", strerror(-err));
150 __connman_iptables_cleanup();