X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=utils.c;h=1b9d6722b406b1144804331ff9d8572e7e0de555;hp=1ba25fdcc0e2c76784e61a17b6146d55c916557c;hb=8eb517c56988da4c5a4c755672ae8f99e97bb4b2;hpb=bb6873d86322a66bc01fc4195512ba5d2c78bddb diff --git a/utils.c b/utils.c index 1ba25fd..1b9d672 100644 --- a/utils.c +++ b/utils.c @@ -19,10 +19,17 @@ #include "utils.h" #include "options.h" +#include "zones.h" +#include "ipsets.h" + + static int lock_fd = -1; static pid_t pipe_pid = -1; static FILE *pipe_fd = NULL; +bool fw3_pr_debug = false; + + static void warn_elem_section_name(struct uci_section *s, bool find_name) { @@ -246,10 +253,18 @@ __fw3_command_pipe(bool silent, const char *command, ...) void fw3_pr(const char *fmt, ...) { - va_list args; - va_start(args, fmt); - vfprintf(pipe_fd, fmt, args); - va_end(args); + va_list args; + + if (fw3_pr_debug && pipe_fd != stdout) + { + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + } + + va_start(args, fmt); + vfprintf(pipe_fd, fmt, args); + va_end(args); } void @@ -332,66 +347,90 @@ fw3_unlock(void) } -struct list_head * -fw3_read_statefile(void) +bool +fw3_read_statefile(void *state) { FILE *sf; - int n; + int n, type; char line[128]; - const char *p; + const char *p, *name; - struct list_head *state; - struct fw3_statefile_entry *entry; + uint16_t flags[2]; + + struct fw3_state *s = state; + struct fw3_zone *zone; + struct fw3_ipset *ipset; sf = fopen(FW3_STATEFILE, "r"); if (!sf) - return NULL; - - state = malloc(sizeof(*state)); - - if (!state) - return NULL; - - INIT_LIST_HEAD(state); + return false; while (fgets(line, sizeof(line), sf)) { - entry = malloc(sizeof(*entry)); - - if (!entry) - continue; - - memset(entry, 0, sizeof(*entry)); - p = strtok(line, " \t\n"); if (!p) continue; - entry->type = strtoul(p, NULL, 10); - - p = strtok(NULL, " \t\n"); + type = strtoul(p, NULL, 10); + name = strtok(NULL, " \t\n"); - if (!p) + if (!name) continue; - entry->name = strdup(p); - for (n = 0, p = strtok(NULL, " \t\n"); - n < ARRAY_SIZE(entry->flags) && p != NULL; + n < ARRAY_SIZE(flags) && p != NULL; n++, p = strtok(NULL, " \t\n")) { - entry->flags[n] = strtoul(p, NULL, 10); + flags[n] = strtoul(p, NULL, 10); } - list_add_tail(&entry->list, state); + switch (type) + { + case FW3_TYPE_DEFAULTS: + s->running_defaults.flags = flags[0]; + break; + + case FW3_TYPE_ZONE: + if (!(zone = fw3_lookup_zone(state, name, false))) + { + zone = fw3_alloc_zone(); + + if (!zone) + continue; + + zone->name = strdup(name); + list_add_tail(&zone->list, &s->zones); + } + + zone->src_flags = flags[0]; + zone->dst_flags = flags[1]; + list_add_tail(&zone->running_list, &s->running_zones); + break; + + case FW3_TYPE_IPSET: + if (!(ipset = fw3_lookup_ipset(state, name, false))) + { + ipset = fw3_alloc_ipset(); + + if (!ipset) + continue; + + ipset->name = strdup(name); + list_add_tail(&ipset->list, &s->ipsets); + } + + ipset->flags = flags[0]; + list_add_tail(&ipset->running_list, &s->running_ipsets); + break; + } } fclose(sf); - return state; + return true; } void @@ -424,40 +463,56 @@ fw3_write_statefile(void *state) fprintf(sf, "%u - %u\n", FW3_TYPE_DEFAULTS, d->flags); - list_for_each_entry(z, &s->zones, list) + list_for_each_entry(z, &s->running_zones, running_list) { fprintf(sf, "%u %s %u %u\n", FW3_TYPE_ZONE, z->name, z->src_flags, z->dst_flags); } - list_for_each_entry(i, &s->ipsets, list) + list_for_each_entry(i, &s->running_ipsets, running_list) { - if (i->external && *i->external) - continue; - - if (!i->flags) - continue; - fprintf(sf, "%u %s %u\n", FW3_TYPE_IPSET, i->name, i->flags); } fclose(sf); } + +struct object_list_heads +{ + struct list_head list; + struct list_head running_list; +}; + void -fw3_free_statefile(struct list_head *statefile) +fw3_set_running(void *object, struct list_head *dest) { - struct fw3_statefile_entry *e, *tmp; + struct object_list_heads *o = object; - if (!statefile) - return; + if (dest && !o->running_list.next) + list_add_tail(&o->running_list, dest); + else if (!dest && o->running_list.next) + list_del(&o->running_list); +} - list_for_each_entry_safe(e, tmp, statefile, list) +void +fw3_free_object(void *obj, const void *opts) +{ + const struct fw3_option *ol; + struct list_head *list, *cur, *tmp; + + for (ol = opts; ol->name; ol++) { - list_del(&e->list); - free(e->name); - free(e); + if (!ol->elem_size) + continue; + + list = (struct list_head *)((char *)obj + ol->offset); + list_for_each_safe(cur, tmp, list) + { + list_del(cur); + free(cur); + } } - free(statefile); + free(obj); }