From: Pierre Lebleu Date: Thu, 4 May 2017 08:52:53 +0000 (+0200) Subject: firewall3: add fw3_attr_parse_name_type() function X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=d44f4189b6a6eb5e943f11dfff0246d3ac0e9087;p=oweals%2Ffirewall3.git firewall3: add fw3_attr_parse_name_type() function Move the name and type parsing out of the rule file in order to make it reusable by others. Signed-off-by: Pierre Lebleu --- 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 875a141..024f95e 100644 --- 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 98e1eec..9a716ae 100644 --- a/utils.h +++ b/utils.h @@ -32,6 +32,7 @@ #include #include +#include #include @@ -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