[LEDE-DEV] [PATCH v2 3/9] firewall3: add fw3_attr_parse_name_type() function
pme.lebleu at gmail.com
pme.lebleu at gmail.com
Thu May 4 01:52:53 PDT 2017
From: Pierre Lebleu <pme.lebleu at gmail.com>
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 at gmail.com>
---
rules.c | 16 +++++++---------
snats.c | 16 +++++++---------
utils.c | 19 +++++++++++++++++++
utils.h | 3 +++
4 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/rules.c b/rules.c
index d34fb7e..9867082 100644
--- 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 fad6008..e392e08 100644
--- 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 537c629..3a4a91f 100644
--- a/utils.c
+++ b/utils.c
@@ -890,3 +890,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 98e1eec..9a716ae 100644
--- 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
--
1.7.9.5
More information about the Lede-dev
mailing list