X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=utils.c;h=2747a853a8b99f9b4e021b2c867092f8fd4d3156;hp=4691fe154c38a55f914cdfd7b48d1842dde749fd;hb=c7fc65809ae5300f256a1228f7011a1dc1bc85ac;hpb=ea1e5c25c1c4c8c82b51c0440d033944ccb4e2e2 diff --git a/utils.c b/utils.c index 4691fe1..2747a85 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 @@ -333,88 +348,92 @@ fw3_unlock(void) bool -fw3_has_state(void) -{ - struct stat s; - return !stat(FW3_STATEFILE, &s); -} - -struct list_head * -fw3_read_state(void) +fw3_read_statefile(void *state) { FILE *sf; - int n; + int type; char line[128]; - const char *p; - - struct list_head *state; - struct fw3_statefile_entry *entry; - - state = malloc(sizeof(*state)); + const char *p, *name; - if (!state) - return NULL; + uint32_t flags; - INIT_LIST_HEAD(state); + struct fw3_state *s = state; + struct fw3_zone *zone; + struct fw3_ipset *ipset; sf = fopen(FW3_STATEFILE, "r"); if (!sf) - { - warn("Cannot open state %s: %s", FW3_STATEFILE, strerror(errno)); - free(state); - - return NULL; - } + 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); + type = strtoul(p, NULL, 16); + name = strtok(NULL, " \t\n"); + + if (!name) + continue; p = strtok(NULL, " \t\n"); if (!p) continue; - entry->name = strdup(p); + flags = strtoul(p, NULL, 16); - for (n = 0, p = strtok(NULL, " \t\n"); - n < ARRAY_SIZE(entry->flags) && p != NULL; - n++, p = strtok(NULL, " \t\n")) + switch (type) { - entry->flags[n] = strtoul(p, NULL, 10); - } + case FW3_TYPE_DEFAULTS: + s->defaults.running_flags = flags; + break; - list_add_tail(&entry->list, state); + 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->running_flags = flags; + 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->running_flags = flags; + list_add_tail(&ipset->running_list, &s->running_ipsets); + break; + } } fclose(sf); - return state; -} - -void -fw3_free_state(struct list_head *statefile) -{ - fw3_free_list(statefile); - free(statefile); + return true; } void -fw3_write_state(void *state) +fw3_write_statefile(void *state) { FILE *sf; struct fw3_state *s = state; @@ -422,6 +441,15 @@ fw3_write_state(void *state) struct fw3_zone *z; struct fw3_ipset *i; + if (fw3_no_family(d)) + { + if (unlink(FW3_STATEFILE)) + warn("Unable to remove state %s: %s", + FW3_STATEFILE, strerror(errno)); + + return; + } + sf = fopen(FW3_STATEFILE, "w"); if (!sf) @@ -430,28 +458,57 @@ fw3_write_state(void *state) return; } - fprintf(sf, "%u - %u\n", FW3_TYPE_DEFAULTS, d->has_flag); + fprintf(sf, "%x - %x\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->has_src_target, z->has_dest_target); + fprintf(sf, "%x %s %x\n", FW3_TYPE_ZONE, z->name, z->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; - - fprintf(sf, "%u %s\n", FW3_TYPE_IPSET, i->name); + fprintf(sf, "%x %s %x\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_set_running(void *object, struct list_head *dest) +{ + struct object_list_heads *o = object; + + 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); +} + void -fw3_remove_state(void) +fw3_free_object(void *obj, const void *opts) { - if (unlink(FW3_STATEFILE)) - warn("Unable to remove state %s: %s", FW3_STATEFILE, strerror(errno)); + const struct fw3_option *ol; + struct list_head *list, *cur, *tmp; + + for (ol = opts; ol->name; ol++) + { + 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(obj); }