firewall3: add fw3_attr_parse_name_type() function
authorPierre Lebleu <pme.lebleu@gmail.com>
Thu, 4 May 2017 08:52:53 +0000 (10:52 +0200)
committerJo-Philipp Wich <jo@mein.io>
Tue, 9 May 2017 21:54:15 +0000 (23:54 +0200)
Move the name and type parsing out of the rule file
in order to make it reusable by others.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
rules.c
snats.c
utils.c
utils.h

diff --git a/rules.c b/rules.c
index d34fb7e489dd538432f172380aa27b65add9dab6..98670821dc336fe4c8bac7ada5add517ce8ef7e1 100644 (file)
--- a/rules.c
+++ b/rules.c
@@ -202,21 +202,19 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p,
        struct uci_section *s;
        struct uci_element *e;
        struct fw3_rule *rule;
-       struct blob_attr *entry, *opt;
-       unsigned rem, orem;
+       struct blob_attr *entry;
+       unsigned rem;
 
        INIT_LIST_HEAD(&state->rules);
 
        blob_for_each_attr(entry, a, rem) {
-               const char *type = NULL;
+               const char *type;
                const char *name = "ubus rule";
-               blobmsg_for_each_attr(opt, entry, orem)
-                       if (!strcmp(blobmsg_name(opt), "type"))
-                               type = blobmsg_get_string(opt);
-                       else if (!strcmp(blobmsg_name(opt), "name"))
-                               name = blobmsg_get_string(opt);
 
-               if (!type || strcmp(type, "rule"))
+               if (!fw3_attr_parse_name_type(entry, &name, &type))
+                       continue;
+
+               if (strcmp(type, "rule"))
                        continue;
 
                if (!(rule = alloc_rule(state)))
diff --git a/snats.c b/snats.c
index fad600876f4d8bc7c64f9384b87c32fb12dac1bb..e392e08692e884019094707a39182fa4c8728c17 100644 (file)
--- a/snats.c
+++ b/snats.c
@@ -126,21 +126,19 @@ fw3_load_snats(struct fw3_state *state, struct uci_package *p, struct blob_attr
        struct uci_section *s;
        struct uci_element *e;
        struct fw3_snat *snat, *n;
-       struct blob_attr *rule, *opt;
+       struct blob_attr *entry, *opt;
        unsigned rem, orem;
 
        INIT_LIST_HEAD(&state->snats);
 
-       blob_for_each_attr(rule, a, rem) {
+       blob_for_each_attr(entry, a, rem) {
                const char *type = NULL;
                const char *name = "ubus rule";
-               blobmsg_for_each_attr(opt, rule, orem)
-                       if (!strcmp(blobmsg_name(opt), "type"))
-                               type = blobmsg_get_string(opt);
-                       else if (!strcmp(blobmsg_name(opt), "name"))
-                               name = blobmsg_get_string(opt);
 
-               if (!type || strcmp(type, "nat"))
+               if (!fw3_attr_parse_name_type(entry, &name, &type))
+                       continue;
+
+               if (strcmp(type, "nat"))
                        continue;
 
                if (!(snat = alloc_snat(state)))
@@ -148,7 +146,7 @@ fw3_load_snats(struct fw3_state *state, struct uci_package *p, struct blob_attr
 
                if (!fw3_parse_blob_options(snat, fw3_snat_opts, rule, name))
                {
-                       fprintf(stderr, "%s skipped due to invalid options\n", name);
+                       warn_section("nat", snat, NULL, "skipped due to invalid options");
                        fw3_free_snat(snat);
                        continue;
                }
diff --git a/utils.c b/utils.c
index 875a141cc44fa2e2efb51ca8f3da673d77a5b1d6..024f95e16d5df1e1d2cc2adb399f42e589c91000 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -893,3 +893,22 @@ fw3_flush_conntrack(void *state)
 
        freeifaddrs(ifaddr);
 }
+
+bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type)
+{
+       struct blob_attr *opt;
+       unsigned orem;
+
+       if (!type || !name)
+               return false;
+
+       *type = NULL;
+
+       blobmsg_for_each_attr(opt, entry, orem)
+               if (!strcmp(blobmsg_name(opt), "type"))
+                       *type = blobmsg_get_string(opt);
+               else if (!strcmp(blobmsg_name(opt), "name"))
+                       *name = blobmsg_get_string(opt);
+
+       return *type != NULL ? true : false;
+}
diff --git a/utils.h b/utils.h
index 98e1eec937333e688110c6b51c57d208931f73dd..9a716aec1a9dd8dc03fff00aeaf4cb9d304a1c5c 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -32,6 +32,7 @@
 #include <ifaddrs.h>
 
 #include <libubox/list.h>
+#include <libubox/blob.h>
 #include <uci.h>
 
 
@@ -113,4 +114,6 @@ bool fw3_bitlen2netmask(int family, int bits, void *mask);
 
 void fw3_flush_conntrack(void *zone);
 
+bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type);
+
 #endif