print a notification if forwards are skipped due to zone family mismatch
[project/firewall3.git] / utils.c
diff --git a/utils.c b/utils.c
index 5198305..9b62789 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -332,32 +332,102 @@ fw3_unlock(void)
 }
 
 
-bool fw3_has_state(void)
+struct list_head *
+fw3_read_statefile(void)
 {
-       struct stat s;
-       return !stat(FW3_STATEFILE, &s);
+       FILE *sf;
+
+       int n;
+       char line[128];
+       const char *p;
+
+       struct list_head *state;
+       struct fw3_statefile_entry *entry;
+
+       sf = fopen(FW3_STATEFILE, "r");
+
+       if (!sf)
+               return NULL;
+
+       state = malloc(sizeof(*state));
+
+       if (!state)
+               return NULL;
+
+       INIT_LIST_HEAD(state);
+
+       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");
+
+               if (!p)
+                       continue;
+
+               entry->name = strdup(p);
+
+               for (n = 0, p = strtok(NULL, " \t\n");
+                    n < ARRAY_SIZE(entry->flags) && p != NULL;
+                    n++, p = strtok(NULL, " \t\n"))
+               {
+                       entry->flags[n] = strtoul(p, NULL, 10);
+               }
+
+               list_add_tail(&entry->list, state);
+       }
+
+       fclose(sf);
+
+       return state;
 }
 
-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))
+       {
+               if (unlink(FW3_STATEFILE))
+                       warn("Unable to remove state %s: %s",
+                            FW3_STATEFILE, strerror(errno));
+
+               return;
+       }
+
+       sf = fopen(FW3_STATEFILE, "w");
+
+       if (!sf)
        {
                warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
                return;
        }
 
+       fprintf(sf, "%u - %u\n", FW3_TYPE_DEFAULTS, d->flags);
+
        list_for_each_entry(z, &s->zones, list)
        {
-               write(fd, "zone ", 5);
-               write(fd, z->name, strlen(z->name));
-               write(fd, "\n", 1);
+               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)
@@ -365,16 +435,26 @@ void fw3_write_state(void *state)
                if (i->external && *i->external)
                        continue;
 
-               write(fd, "ipset ", 6);
-               write(fd, i->name, strlen(i->name));
-               write(fd, "\n", 1);
+               fprintf(sf, "%u %s %u\n", FW3_TYPE_IPSET, i->name, i->flags);
        }
 
-       close(fd);
+       fclose(sf);
 }
 
-void fw3_remove_state(void)
+void
+fw3_free_statefile(struct list_head *statefile)
 {
-       if (unlink(FW3_STATEFILE))
-               warn("Unable to remove state %s: %s", FW3_STATEFILE, strerror(errno));
+       struct fw3_statefile_entry *e, *tmp;
+
+       if (!statefile)
+               return;
+
+       list_for_each_entry_safe(e, tmp, statefile, list)
+       {
+               list_del(&e->list);
+               free(e->name);
+               free(e);
+       }
+
+       free(statefile);
 }