X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=ubus.c;h=5161922144fb90287b7d453f84effd584b111938;hp=877104ad29aeb2cafa454a9532b95c05d1036296;hb=fb51b4bdea243b2131dce324cb0b71be339a1ab4;hpb=203d616e3ce62d9d8cd3b2e11b8a757c97d7da0f diff --git a/ubus.c b/ubus.c index 877104a..5161922 100644 --- a/ubus.c +++ b/ubus.c @@ -19,6 +19,7 @@ #include "ubus.h" static struct blob_attr *interfaces = NULL; +static struct blob_attr *procd_data; static void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg) @@ -31,12 +32,18 @@ static void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg) interfaces = blob_memdup(cur); } +static void procd_data_cb(struct ubus_request *req, int type, struct blob_attr *msg) +{ + procd_data = blob_memdup(msg); +} + bool fw3_ubus_connect(void) { bool status = false; uint32_t id; struct ubus_context *ctx = ubus_connect(NULL); + struct blob_buf b = { }; if (!ctx) goto out; @@ -49,6 +56,14 @@ fw3_ubus_connect(void) status = true; + if (ubus_lookup_id(ctx, "service", &id)) + goto out; + + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "type", "firewall"); + ubus_invoke(ctx, id, "get_data", b.head, procd_data_cb, NULL, 2000); + blob_buf_free(&b); + out: if (ctx) ubus_free(ctx); @@ -90,16 +105,15 @@ parse_subnet(enum fw3_family family, struct blob_attr *dict, int rem) static void parse_subnets(struct list_head *head, enum fw3_family family, - struct blob_attr *list) + struct blob_attr *list, int rem) { struct blob_attr *cur; struct fw3_address *addr; - int rem; if (!list) return; - blob_for_each_attr(cur, list, rem) + __blob_for_each_attr(cur, list, rem) { addr = parse_subnet(family, blobmsg_data(cur), blobmsg_data_len(cur)); @@ -125,18 +139,13 @@ fw3_ubus_device(const char *net) struct fw3_device *dev = NULL; struct blob_attr *tb[__DEV_MAX]; struct blob_attr *cur; + char *name = NULL; int rem; if (!net || !interfaces) return NULL; - dev = calloc(1, sizeof(*dev)); - if (!dev) - return NULL; - blobmsg_for_each_attr(cur, interfaces, rem) { - char *name; - blobmsg_parse(policy, __DEV_MAX, tb, blobmsg_data(cur), blobmsg_len(cur)); if (!tb[DEV_INTERFACE] || strcmp(blobmsg_data(tb[DEV_INTERFACE]), net) != 0) @@ -149,12 +158,21 @@ fw3_ubus_device(const char *net) else continue; - snprintf(dev->name, sizeof(dev->name), "%s", name); - dev->set = !!dev->name[0]; - return dev; + break; } - return NULL; + if (!name) + return NULL; + + dev = calloc(1, sizeof(*dev)); + + if (!dev) + return NULL; + + snprintf(dev->name, sizeof(dev->name), "%s", name); + dev->set = true; + + return dev; } void @@ -187,9 +205,9 @@ fw3_ubus_address(struct list_head *list, const char *net) strcmp(blobmsg_data(tb[ADDR_INTERFACE]), net) != 0) continue; - parse_subnets(list, FW3_FAMILY_V4, tb[ADDR_IPV4]); - parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6]); - parse_subnets(list, FW3_FAMILY_V6, tb[ADDR_IPV6_PREFIX]); + parse_subnets(list, FW3_FAMILY_V4, blobmsg_data(tb[ADDR_IPV4]), blobmsg_data_len(tb[ADDR_IPV4])); + parse_subnets(list, FW3_FAMILY_V6, blobmsg_data(tb[ADDR_IPV6]), blobmsg_data_len(tb[ADDR_IPV6])); + parse_subnets(list, FW3_FAMILY_V6, blobmsg_data(tb[ADDR_IPV6_PREFIX]), blobmsg_data_len(tb[ADDR_IPV6_PREFIX])); } } @@ -257,4 +275,28 @@ fw3_ubus_rules(struct blob_buf *b) } } } + + if (!procd_data) + return; + + /* service */ + blobmsg_for_each_attr(c, procd_data, r) { + if (!blobmsg_check_attr(c, true)) + continue; + + /* instance */ + blobmsg_for_each_attr(cur, c, rem) { + if (!blobmsg_check_attr(cur, true)) + continue; + + /* type */ + blobmsg_for_each_attr(dcur, cur, drem) { + if (!blobmsg_check_attr(dcur, true)) + continue; + + blobmsg_for_each_attr(rule, dcur, rrem) + blobmsg_add_blob(b, rule); + } + } + } }