X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=utils.c;h=34464ee638c1e04c028a337469cd32b125c3f335;hp=5198305214ccf0435f0c71c5cc131ef016e06454;hb=bd574af529c0661c125336bdd9d0d1f2e09287c3;hpb=a32e331a11034403df2e26807df9195435b6fb8a diff --git a/utils.c b/utils.c index 5198305..34464ee 100644 --- a/utils.c +++ b/utils.c @@ -19,6 +19,10 @@ #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; @@ -332,49 +336,172 @@ fw3_unlock(void) } -bool fw3_has_state(void) +bool +fw3_read_statefile(void *state) { - struct stat s; - return !stat(FW3_STATEFILE, &s); + FILE *sf; + + int n, type; + char line[128]; + const char *p, *name; + + 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 false; + + while (fgets(line, sizeof(line), sf)) + { + p = strtok(line, " \t\n"); + + if (!p) + continue; + + type = strtoul(p, NULL, 10); + name = strtok(NULL, " \t\n"); + + if (!name) + continue; + + for (n = 0, p = strtok(NULL, " \t\n"); + n < ARRAY_SIZE(flags) && p != NULL; + n++, p = strtok(NULL, " \t\n")) + { + flags[n] = strtoul(p, NULL, 10); + } + + 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 true; } -void fw3_write_state(void *state) +void +fw3_write_statefile(void *state) { - int fd; + FILE *sf; struct fw3_state *s = state; + struct fw3_defaults *d = &s->defaults; struct fw3_zone *z; struct fw3_ipset *i; - fd = open(FW3_STATEFILE, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR); + int mask = (1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6); - if (fd < 0) + if (!(d->flags & mask)) { - warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno)); + if (unlink(FW3_STATEFILE)) + warn("Unable to remove state %s: %s", + FW3_STATEFILE, strerror(errno)); + return; } - list_for_each_entry(z, &s->zones, list) + sf = fopen(FW3_STATEFILE, "w"); + + if (!sf) { - write(fd, "zone ", 5); - write(fd, z->name, strlen(z->name)); - write(fd, "\n", 1); + warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno)); + return; } - list_for_each_entry(i, &s->ipsets, list) + fprintf(sf, "%u - %u\n", FW3_TYPE_DEFAULTS, d->flags); + + list_for_each_entry(z, &s->running_zones, running_list) { - if (i->external && *i->external) - continue; + fprintf(sf, "%u %s %u %u\n", FW3_TYPE_ZONE, + z->name, z->src_flags, z->dst_flags); + } - write(fd, "ipset ", 6); - write(fd, i->name, strlen(i->name)); - write(fd, "\n", 1); + list_for_each_entry(i, &s->running_ipsets, running_list) + { + fprintf(sf, "%u %s %u\n", FW3_TYPE_IPSET, i->name, i->flags); } - close(fd); + fclose(sf); } -void fw3_remove_state(void) + +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_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); }