diff options
author | Pierre Lebleu <pme.lebleu@gmail.com> | 2017-05-04 10:52:53 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2017-05-09 23:54:15 +0200 |
commit | d44f4189b6a6eb5e943f11dfff0246d3ac0e9087 (patch) | |
tree | 634b0258d40048d2cbe251badd4061d0db076ada | |
parent | e264c8e585ea37ccb1739e7a8e12f8454da1d8a4 (diff) | |
download | firewall3-d44f4189b6a6eb5e943f11dfff0246d3ac0e9087.tar.gz |
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 <pme.lebleu@gmail.com>
-rw-r--r-- | rules.c | 16 | ||||
-rw-r--r-- | snats.c | 16 | ||||
-rw-r--r-- | utils.c | 19 | ||||
-rw-r--r-- | utils.h | 3 |
4 files changed, 36 insertions, 18 deletions
@@ -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))) @@ -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; } @@ -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; +} @@ -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 |