record used zone chains in state file
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 17 Feb 2013 23:25:48 +0000 (00:25 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 17 Feb 2013 23:25:48 +0000 (00:25 +0100)
utils.c

diff --git a/utils.c b/utils.c
index 5198305214ccf0435f0c71c5cc131ef016e06454..a5d0082415dfb1152dc47d7fda94e1cd20ed1a51 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -340,14 +340,15 @@ bool fw3_has_state(void)
 
 void fw3_write_state(void *state)
 {
-       int fd;
+       FILE *sf;
+       int n, val;
        struct fw3_state *s = state;
        struct fw3_zone *z;
        struct fw3_ipset *i;
 
-       fd = open(FW3_STATEFILE, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
+       sf = fopen(FW3_STATEFILE, "w");
 
-       if (fd < 0)
+       if (!sf)
        {
                warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
                return;
@@ -355,9 +356,17 @@ void fw3_write_state(void *state)
 
        list_for_each_entry(z, &s->zones, list)
        {
-               write(fd, "zone ", 5);
-               write(fd, z->name, strlen(z->name));
-               write(fd, "\n", 1);
+               for (n = FW3_TARGET_ACCEPT, val = 0; n <= FW3_TARGET_SNAT; n++)
+                       if (z->has_src_target[n])
+                               val |= (1 << n);
+
+               fprintf(sf, "zone %s %u", z->name, val);
+
+               for (n = FW3_TARGET_ACCEPT, val = 0; n <= FW3_TARGET_SNAT; n++)
+                       if (z->has_dest_target[n])
+                               val |= (1 << n);
+
+               fprintf(sf, " %u\n", val);
        }
 
        list_for_each_entry(i, &s->ipsets, list)
@@ -365,12 +374,10 @@ 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, "ipset %s\n", i->name);
        }
 
-       close(fd);
+       fclose(sf);
 }
 
 void fw3_remove_state(void)