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_INSERT,
34 IPTABLES_COMMAND_DELETE,
35 IPTABLES_COMMAND_POLICY,
36 IPTABLES_COMMAND_CHAIN_INSERT,
37 IPTABLES_COMMAND_CHAIN_DELETE,
38 IPTABLES_COMMAND_CHAIN_FLUSH,
39 IPTABLES_COMMAND_DUMP,
40 IPTABLES_COMMAND_UNKNOWN,
43 int main(int argc, char *argv[])
45 enum iptables_command cmd = IPTABLES_COMMAND_UNKNOWN;
46 char *table = NULL, *chain = NULL, *rule = NULL, *tmp;
51 while ((c = getopt_long(argc, argv,
52 "-A:I:D:P:N:X:F:Lt:", NULL, NULL)) != -1) {
56 cmd = IPTABLES_COMMAND_APPEND;
60 cmd = IPTABLES_COMMAND_INSERT;
64 cmd = IPTABLES_COMMAND_DELETE;
68 /* The policy will be stored in rule. */
69 cmd = IPTABLES_COMMAND_POLICY;
73 cmd = IPTABLES_COMMAND_CHAIN_INSERT;
77 cmd = IPTABLES_COMMAND_CHAIN_DELETE;
81 cmd = IPTABLES_COMMAND_CHAIN_FLUSH;
84 cmd = IPTABLES_COMMAND_DUMP;
98 for (i = optind - 1; i < argc; i++) {
101 rule = g_strdup_printf("%s %s", rule, argv[i]);
104 rule = g_strdup(argv[i]);
107 __connman_iptables_init();
110 case IPTABLES_COMMAND_APPEND:
111 err = __connman_iptables_append(table, chain, rule);
113 case IPTABLES_COMMAND_INSERT:
114 err = __connman_iptables_insert(table, chain, rule);
116 case IPTABLES_COMMAND_DELETE:
117 err = __connman_iptables_delete(table, chain, rule);
119 case IPTABLES_COMMAND_POLICY:
120 err = __connman_iptables_change_policy(table, chain, rule);
122 case IPTABLES_COMMAND_CHAIN_INSERT:
123 err = __connman_iptables_new_chain(table, chain);
125 case IPTABLES_COMMAND_CHAIN_DELETE:
126 err = __connman_iptables_delete_chain(table, chain);
128 case IPTABLES_COMMAND_CHAIN_FLUSH:
129 err = __connman_iptables_flush_chain(table, chain);
131 case IPTABLES_COMMAND_DUMP:
132 __connman_log_init(argv[0], "*", false, false,
133 "iptables-test", "1");
134 err = __connman_iptables_dump(table);
136 case IPTABLES_COMMAND_UNKNOWN:
137 printf("Missing command\n");
138 printf("usage: iptables-test [-t table] {-A|-I|-D} chain rule\n");
139 printf(" iptables-test [-t table] {-N|-X|-F} chain\n");
140 printf(" iptables-test [-t table] -L\n");
141 printf(" iptables-test [-t table] -P chain target\n");
146 printf("Error: %s\n", strerror(-err));
150 err = __connman_iptables_commit(table);
152 printf("Failed to commit changes: %s\n", strerror(-err));
158 __connman_iptables_cleanup();