Unify print_chains() implementations in utils.c fw3_pr_rulespec()
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 12 Mar 2013 15:08:46 +0000 (16:08 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 12 Mar 2013 15:14:13 +0000 (16:14 +0100)
defaults.c
utils.c
utils.h
zones.c

index 8bf1aa3..1f0ec89 100644 (file)
 #include "defaults.h"
 
 
-#define C(f, tbl, def, name) \
-       { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_DEFAULT_##def, name }
-
-struct chain {
-       enum fw3_family family;
-       enum fw3_table table;
-       enum fw3_default flag;
-       const char *name;
-};
+#define C(f, tbl, def, fmt) \
+       { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_DEFAULT_##def, fmt }
 
-static const struct chain default_chains[] = {
+static const struct fw3_rule_spec default_chains[] = {
        C(ANY, FILTER, UNSPEC,        "delegate_input"),
        C(ANY, FILTER, UNSPEC,        "delegate_output"),
        C(ANY, FILTER, UNSPEC,        "delegate_forward"),
@@ -46,9 +39,11 @@ static const struct chain default_chains[] = {
 
        C(ANY, MANGLE, UNSPEC,        "mssfix"),
        C(ANY, RAW,    UNSPEC,        "notrack"),
+
+       { }
 };
 
-static const struct chain toplevel_rules[] = {
+static const struct fw3_rule_spec toplevel_rules[] = {
        C(ANY, FILTER, UNSPEC,        "INPUT -j delegate_input"),
        C(ANY, FILTER, UNSPEC,        "OUTPUT -j delegate_output"),
        C(ANY, FILTER, UNSPEC,        "FORWARD -j delegate_forward"),
@@ -58,6 +53,8 @@ static const struct chain toplevel_rules[] = {
 
        C(ANY, MANGLE, UNSPEC,        "FORWARD -j mssfix"),
        C(ANY, RAW,    UNSPEC,        "PREROUTING -j notrack"),
+
+       { }
 };
 
 const struct fw3_option fw3_default_opts[] = {
@@ -86,37 +83,6 @@ const struct fw3_option fw3_default_opts[] = {
 };
 
 
-static bool
-print_chains(enum fw3_table table, enum fw3_family family,
-             const char *fmt, uint32_t *flags, uint32_t mask,
-             const struct chain *chains, int n)
-{
-       bool rv = false;
-       const struct chain *c;
-       uint32_t f = flags ? flags[family == FW3_FAMILY_V6] : 0;
-
-       if (mask)
-               f &= mask;
-
-       for (c = chains; n > 0; c++, n--)
-       {
-               if (!fw3_is_family(c, family))
-                       continue;
-
-               if (c->table != table)
-                       continue;
-
-               if ((c->flag != FW3_DEFAULT_UNSPEC) && !hasbit(f, c->flag))
-                       continue;
-
-               fw3_pr(fmt, c->name);
-
-               rv = true;
-       }
-
-       return rv;
-}
-
 static void
 check_policy(struct uci_element *e, enum fw3_target *pol, const char *name)
 {
@@ -199,8 +165,8 @@ fw3_print_default_chains(enum fw3_table table, enum fw3_family family,
        if (defs->syn_flood)
                set(defs->flags, family, FW3_DEFAULT_SYN_FLOOD);
 
-       rv = print_chains(table, family, ":%s - [0:0]\n", defs->flags, custom_mask,
-                         default_chains, ARRAY_SIZE(default_chains));
+       rv = fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+                            default_chains, ":%s - [0:0]\n");
 
        if (rv)
                set(defs->flags, family, table);
@@ -218,8 +184,7 @@ fw3_print_default_head_rules(enum fw3_table table, enum fw3_family family,
                "forward", "forwarding",
        };
 
-       print_chains(table, family, "-A %s\n", NULL, 0,
-                    toplevel_rules, ARRAY_SIZE(toplevel_rules));
+       fw3_pr_rulespec(table, family, NULL, 0, toplevel_rules, "-A %s\n");
 
        switch (table)
        {
@@ -358,19 +323,16 @@ fw3_flush_rules(enum fw3_table table, enum fw3_family family,
        {
                reset_policy(table, reload ? FW3_TARGET_DROP : FW3_TARGET_ACCEPT);
 
-               print_chains(table, family, "-D %s\n",
-                            defs->flags, custom_mask,
-                                        toplevel_rules, ARRAY_SIZE(toplevel_rules));
+               fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+                               toplevel_rules, "-D %s\n");
 
-               print_chains(table, family, "-F %s\n",
-                            defs->flags, custom_mask,
-                                        default_chains, ARRAY_SIZE(default_chains));
+               fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+                               default_chains, "-F %s\n");
        }
        else
        {
-               print_chains(table, family, "-X %s\n",
-                            defs->flags, custom_mask,
-                                        default_chains, ARRAY_SIZE(default_chains));
+               fw3_pr_rulespec(table, family, defs->flags, custom_mask,
+                               default_chains, "-X %s\n");
 
                del(defs->flags, family, table);
        }
diff --git a/utils.c b/utils.c
index 7f2dd64..4314f91 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -526,3 +526,40 @@ fw3_free_object(void *obj, const void *opts)
 
        free(obj);
 }
+
+
+bool
+fw3_pr_rulespec(int table, int family, uint32_t *flags, uint32_t mask,
+                const struct fw3_rule_spec *r, const char *fmt, ...)
+{
+       char buf[256];
+       bool rv = false;
+
+       va_list ap;
+       uint32_t f = flags ? flags[family == FW3_FAMILY_V6] : 0;
+
+       if (mask)
+               f &= mask;
+
+       for (; r->format; r++)
+       {
+               if (!fw3_is_family(r, family))
+                       continue;
+
+               if (r->table != table)
+                       continue;
+
+               if ((r->flag != 0) && !hasbit(f, r->flag))
+                       continue;
+
+               va_start(ap, fmt);
+               vsnprintf(buf, sizeof(buf), r->format, ap);
+               va_end(ap);
+
+               fw3_pr(fmt, buf);
+
+               rv = true;
+       }
+
+       return rv;
+}
diff --git a/utils.h b/utils.h
index e0af88f..60e87c2 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -97,4 +97,15 @@ void fw3_set_running(void *object, struct list_head *dest);
 
 void fw3_free_object(void *obj, const void *opts);
 
+
+struct fw3_rule_spec {
+       int family;
+       int table;
+       int flag;
+       const char *format;
+};
+
+bool fw3_pr_rulespec(int table, int family, uint32_t *flags, uint32_t mask,
+                     const struct fw3_rule_spec *r, const char *fmt, ...);
+
 #endif
diff --git a/zones.c b/zones.c
index f41cb0f..1506c01 100644 (file)
--- a/zones.c
+++ b/zones.c
 #include "ubus.h"
 
 
-#define C(f, tbl, tgt, name) \
-       { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, name }
-
-struct chain {
-       enum fw3_family family;
-       enum fw3_table table;
-       enum fw3_target target;
-       const char *name;
-};
+#define C(f, tbl, tgt, fmt) \
+       { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, fmt }
 
-static const struct chain zone_chains[] = {
+static const struct fw3_rule_spec zone_chains[] = {
        C(ANY, FILTER, UNSPEC,        "zone_%1$s_input"),
        C(ANY, FILTER, UNSPEC,        "zone_%1$s_output"),
        C(ANY, FILTER, UNSPEC,        "zone_%1$s_forward"),
@@ -52,6 +45,8 @@ static const struct chain zone_chains[] = {
 
        C(V4,  NAT,    CUSTOM_CHAINS, "prerouting_%1$s_rule"),
        C(V4,  NAT,    CUSTOM_CHAINS, "postrouting_%1$s_rule"),
+
+       { }
 };
 
 
@@ -59,13 +54,15 @@ static const struct chain zone_chains[] = {
        "zone_%1$s_" #dir1 " -m comment --comment \"user chain for %1$s " \
        #dir2 "\" -j " #dir2 "_%1$s_rule"
 
-static const struct chain zone_rules[] = {
+static const struct fw3_rule_spec zone_rules[] = {
        C(ANY, FILTER, CUSTOM_CHAINS, R(input, input)),
        C(ANY, FILTER, CUSTOM_CHAINS, R(output, output)),
        C(ANY, FILTER, CUSTOM_CHAINS, R(forward, forwarding)),
 
        C(V4,  NAT,    CUSTOM_CHAINS, R(prerouting, prerouting)),
        C(V4,  NAT,    CUSTOM_CHAINS, R(postrouting, postrouting)),
+
+       { }
 };
 
 const struct fw3_option fw3_zone_opts[] = {
@@ -101,39 +98,6 @@ const struct fw3_option fw3_zone_opts[] = {
 };
 
 
-static bool
-print_chains(enum fw3_table table, enum fw3_family family,
-             const char *fmt, const char *name, uint32_t *targets, uint32_t mask,
-             const struct chain *chains, int n)
-{
-       bool rv = false;
-       char cn[256] = { 0 };
-       const struct chain *c;
-       uint32_t t = targets ? targets[family == FW3_FAMILY_V6] : 0;
-
-       if (mask)
-               t &= mask;
-
-       for (c = chains; n > 0; c++, n--)
-       {
-               if (!fw3_is_family(c, family))
-                       continue;
-
-               if (c->table != table)
-                       continue;
-
-               if ((c->target != FW3_TARGET_UNSPEC) && !hasbit(t, c->target))
-                       continue;
-
-               snprintf(cn, sizeof(cn), c->name, name);
-               fw3_pr(fmt, cn);
-
-               rv = true;
-       }
-
-       return rv;
-}
-
 static void
 check_policy(struct uci_element *e, enum fw3_target *pol, enum fw3_target def,
              const char *name)
@@ -296,13 +260,11 @@ print_zone_chain(enum fw3_table table, enum fw3_family family,
        if (!zone->conntrack && !state->defaults.drop_invalid)
                set(zone->flags, family, FW3_TARGET_NOTRACK);
 
-       c = print_chains(table, family, ":%s - [0:0]\n", zone->name,
-                        zone->flags, custom_mask,
-                        zone_chains, ARRAY_SIZE(zone_chains));
+       c = fw3_pr_rulespec(table, family, zone->flags, custom_mask, zone_chains,
+                           ":%s - [0:0]\n", zone->name);
 
-       r = print_chains(table, family, "-A %s\n", zone->name,
-                        zone->flags, 0,
-                        zone_rules, ARRAY_SIZE(zone_rules));
+       r = fw3_pr_rulespec(table, family, zone->flags, 0, zone_rules,
+                           "-A %s\n", zone->name);
 
        if (c || r)
        {
@@ -545,9 +507,8 @@ fw3_flush_zones(enum fw3_table table, enum fw3_family family,
                if (!has(z->flags, family, table))
                        continue;
 
-               print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
-                            z->name, z->flags, custom_mask,
-                            zone_chains, ARRAY_SIZE(zone_chains));
+               fw3_pr_rulespec(table, family, z->flags, custom_mask, zone_chains,
+                               pass2 ? "-X %s\n" : "-F %s\n", z->name);
 
                if (pass2)
                {