introduce global string array for enum names, remove private arrays
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 19 Feb 2013 18:48:20 +0000 (19:48 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 19 Feb 2013 23:03:16 +0000 (00:03 +0100)
defaults.c
ipsets.c
main.c
options.c
options.h
zones.c

index 3e8ea40f164404fb6e2ccaabd2a80239c1e2959c..2d646953684a551b6ef10d0398289fb68c0402f3 100644 (file)
@@ -180,21 +180,15 @@ fw3_print_default_chains(enum fw3_table table, enum fw3_family family,
                          struct fw3_state *state)
 {
        struct fw3_defaults *defs = &state->defaults;
-       const char *policy[] = {
-               "(bug)",
-               "ACCEPT",
-               "DROP",
-               "DROP",
-               "(bug)",
-               "(bug)",
-               "(bug)",
-       };
+
+#define policy(t) \
+       ((t == FW3_TARGET_REJECT) ? "DROP" : fw3_flag_names[t])
 
        if (table == FW3_TABLE_FILTER)
        {
-               fw3_pr(":INPUT %s [0:0]\n", policy[defs->policy_input]);
-               fw3_pr(":FORWARD %s [0:0]\n", policy[defs->policy_forward]);
-               fw3_pr(":OUTPUT %s [0:0]\n", policy[defs->policy_output]);
+               fw3_pr(":INPUT %s [0:0]\n", policy(defs->policy_input));
+               fw3_pr(":FORWARD %s [0:0]\n", policy(defs->policy_forward));
+               fw3_pr(":OUTPUT %s [0:0]\n", policy(defs->policy_output));
        }
 
        print_chains(table, family, ":%s - [0:0]\n", defs->flags,
index f4253c7bab64b467a5ca59d0a2813678084029ce..ab86133c31be3756a9e751a4e4e2e514dc5119e2 100644 (file)
--- a/ipsets.c
+++ b/ipsets.c
@@ -263,12 +263,6 @@ create_ipset(struct fw3_ipset *ipset)
                "set",
        };
 
-       const char *families[] = {
-               "(bug)",
-               "inet",
-               "inet6",
-       };
-
        if (ipset->external && *ipset->external)
                return;
 
@@ -317,7 +311,7 @@ create_ipset(struct fw3_ipset *ipset)
        }
 
        if (ipset->family != FW3_FAMILY_ANY)
-               fw3_pr(" family %s", families[ipset->family]);
+               fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");
 
        if (ipset->timeout > 0)
                fw3_pr(" timeout %u", ipset->timeout);
diff --git a/main.c b/main.c
index 2dd1f86f02c13d52c001734ddbcd70926e6e1389..d94d0e9e71555aa615993fb0e6ce856be17258b6 100644 (file)
--- a/main.c
+++ b/main.c
 static bool print_rules = false;
 static enum fw3_family use_family = FW3_FAMILY_ANY;
 
-static const char *families[] = {
-       "(bug)",
-       "IPv4",
-       "IPv6",
-};
-
-static const char *tables[] = {
-       "filter",
-       "nat",
-       "mangle",
-       "raw",
-};
-
 
 static struct fw3_state *
 build_state(void)
@@ -113,18 +100,16 @@ free_state(struct fw3_state *state)
 static bool
 restore_pipe(enum fw3_family family, bool silent)
 {
-       const char *cmd[] = {
-               "(bug)",
-               "iptables-restore",
-               "ip6tables-restore",
-       };
+       const char *cmd;
+
+       cmd = (family == FW3_FAMILY_V4) ? "iptables-restore" : "ip6tables-restore";
 
        if (print_rules)
                return fw3_stdout_pipe();
 
-       if (!fw3_command_pipe(silent, cmd[family], "--lenient", "--noflush"))
+       if (!fw3_command_pipe(silent, cmd, "--lenient", "--noflush"))
        {
-               warn("Unable to execute %s", cmd[family]);
+               warn("Unable to execute %s", cmd);
                return false;
        }
 
@@ -197,17 +182,17 @@ stop(struct fw3_state *state, bool complete, bool restart)
                if (!family_used(family) || !restore_pipe(family, true))
                        continue;
 
-               info("Removing %s rules ...", families[family]);
+               info("Removing %s rules ...", fw3_flag_names[family]);
 
                for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
                {
-                       if (!fw3_has_table(family == FW3_FAMILY_V6, tables[table]))
+                       if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
                                continue;
 
                        info(" * %sing %s table",
-                            complete ? "Flush" : "Clear", tables[table]);
+                            complete ? "Flush" : "Clear", fw3_flag_names[table]);
 
-                       fw3_pr("*%s\n", tables[table]);
+                       fw3_pr("*%s\n", fw3_flag_names[table]);
 
                        if (complete)
                        {
@@ -280,21 +265,21 @@ start(struct fw3_state *state, bool restart)
                {
                        warn("The %s firewall appears to be started already. "
                             "If it is indeed empty, remove the %s file and retry.",
-                            families[family], FW3_STATEFILE);
+                            fw3_flag_names[family], FW3_STATEFILE);
 
                        continue;
                }
 
-               info("Constructing %s rules ...", families[family]);
+               info("Constructing %s rules ...", fw3_flag_names[family]);
 
                for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
                {
-                       if (!fw3_has_table(family == FW3_FAMILY_V6, tables[table]))
+                       if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
                                continue;
 
-                       info(" * Populating %s table", tables[table]);
+                       info(" * Populating %s table", fw3_flag_names[table]);
 
-                       fw3_pr("*%s\n", tables[table]);
+                       fw3_pr("*%s\n", fw3_flag_names[table]);
                        fw3_print_default_chains(table, family, state);
                        fw3_print_zone_chains(table, family, state);
                        fw3_print_default_head_rules(table, family, state);
index 5d325fcbb49658c04c3188f64d162f509c3b2f70..1f10050bb14d1117c7c50ef15230c5a42157c141 100644 (file)
--- a/options.c
+++ b/options.c
 
 #include "options.h"
 
+const char *fw3_flag_names[FW3_DEFAULT_DROP_INVALID + 1] = {
+       "filter",
+       "nat",
+       "mangle",
+       "raw",
+
+       "IPv4",
+       "IPv6",
+
+       "ACCEPT",
+       "REJECT",
+       "DROP",
+       "NOTRACK",
+       "DNAT",
+       "SNAT",
+};
+
 bool
 fw3_parse_bool(void *ptr, const char *val)
 {
index ea5082dcdf1b19ae69464fe76128a6447540d1e8..e02f890e566e59697127cc55fce613b3b6d1f983 100644 (file)
--- a/options.h
+++ b/options.h
@@ -79,6 +79,9 @@ enum fw3_default
        FW3_DEFAULT_DROP_INVALID  = 15,
 };
 
+extern const char *fw3_flag_names[FW3_DEFAULT_DROP_INVALID + 1];
+
+
 enum fw3_limit_unit
 {
        FW3_LIMIT_UNIT_SECOND = 0,
diff --git a/zones.c b/zones.c
index fbde74ef63de70a72c227338bd5157482f9853f9..62d9e369d00b93c89af42837789588e141155c78 100644 (file)
--- a/zones.c
+++ b/zones.c
@@ -242,12 +242,9 @@ print_interface_rule(enum fw3_table table, enum fw3_family family,
                      struct fw3_address *sub, bool disable_notrack)
 {
        enum fw3_target t;
-       const char *targets[] = {
-               "(bug)",  "(bug)",
-               "ACCEPT", "ACCEPT",
-               "REJECT", "reject",
-               "DROP",   "DROP",
-       };
+
+#define jump_target(t) \
+       ((t == FW3_TARGET_REJECT) ? "reject" : fw3_flag_names[t])
 
        if (table == FW3_TABLE_FILTER)
        {
@@ -255,20 +252,20 @@ print_interface_rule(enum fw3_table table, enum fw3_family family,
                {
                        if (hasbit(zone->src_flags, t))
                        {
-                               fw3_pr("-A zone_%s_src_%s", zone->name, targets[t*2]);
+                               fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
                                fw3_format_in_out(dev, NULL);
                                fw3_format_src_dest(sub, NULL);
                                fw3_format_extra(zone->extra_src);
-                               fw3_pr(" -j %s\n", targets[t*2+1]);
+                               fw3_pr(" -j %s\n", jump_target(t));
                        }
 
                        if (hasbit(zone->dst_flags, t))
                        {
-                               fw3_pr("-A zone_%s_dest_%s", zone->name, targets[t*2]);
+                               fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
                                fw3_format_in_out(NULL, dev);
                                fw3_format_src_dest(NULL, sub);
                                fw3_format_extra(zone->extra_dest);
-                               fw3_pr(" -j %s\n", targets[t*2+1]);
+                               fw3_pr(" -j %s\n", jump_target(t));
                        }
                }
 
@@ -375,15 +372,6 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
        struct fw3_address *mdest;
 
        enum fw3_target t;
-       const char *targets[] = {
-               "(bug)",
-               "ACCEPT",
-               "REJECT",
-               "DROP",
-               "(bug)",
-               "(bug)",
-               "(bug)",
-       };
 
        if (!fw3_is_family(zone, family))
                return;
@@ -392,13 +380,13 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
        {
        case FW3_TABLE_FILTER:
                fw3_pr("-A zone_%s_input -j zone_%s_src_%s\n",
-                          zone->name, zone->name, targets[zone->policy_input]);
+                          zone->name, zone->name, fw3_flag_names[zone->policy_input]);
 
                fw3_pr("-A zone_%s_forward -j zone_%s_dest_%s\n",
-                          zone->name, zone->name, targets[zone->policy_forward]);
+                          zone->name, zone->name, fw3_flag_names[zone->policy_forward]);
 
                fw3_pr("-A zone_%s_output -j zone_%s_dest_%s\n",
-                          zone->name, zone->name, targets[zone->policy_output]);
+                          zone->name, zone->name, fw3_flag_names[zone->policy_output]);
 
                if (zone->log)
                {
@@ -406,18 +394,18 @@ print_zone_rule(enum fw3_table table, enum fw3_family family,
                        {
                                if (hasbit(zone->src_flags, t))
                                {
-                                       fw3_pr("-A zone_%s_src_%s", zone->name, targets[t]);
+                                       fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
                                        fw3_format_limit(&zone->log_limit);
                                        fw3_pr(" -j LOG --log-prefix \"%s(src %s)\"\n",
-                                                  targets[t], zone->name);
+                                                  fw3_flag_names[t], zone->name);
                                }
 
                                if (hasbit(zone->dst_flags, t))
                                {
-                                       fw3_pr("-A zone_%s_dest_%s", zone->name, targets[t]);
+                                       fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
                                        fw3_format_limit(&zone->log_limit);
                                        fw3_pr(" -j LOG --log-prefix \"%s(dest %s)\"\n",
-                                                  targets[t], zone->name);
+                                                  fw3_flag_names[t], zone->name);
                                }
                        }
                }