run/load includes on start
[project/firewall3.git] / main.c
diff --git a/main.c b/main.c
index 62f51e1..6eb002f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -26,6 +26,7 @@
 #include "redirects.h"
 #include "forwards.h"
 #include "ipsets.h"
+#include "includes.h"
 #include "ubus.h"
 
 
@@ -39,6 +40,9 @@ build_state(void)
        struct fw3_state *state = NULL;
        struct uci_package *p = NULL;
 
+       if (!fw3_ubus_connect())
+               error("Failed to connect to ubus");
+
        state = malloc(sizeof(*state));
 
        if (!state)
@@ -71,6 +75,7 @@ build_state(void)
        fw3_load_rules(state, p);
        fw3_load_redirects(state, p);
        fw3_load_forwards(state, p);
+       fw3_load_includes(state, p);
 
        state->statefile = fw3_read_statefile(state);
 
@@ -97,6 +102,9 @@ free_state(struct fw3_state *state)
        list_for_each_safe(cur, tmp, &state->ipsets)
                fw3_free_ipset((struct fw3_ipset *)cur);
 
+       list_for_each_safe(cur, tmp, &state->includes)
+               fw3_free_include((struct fw3_include *)cur);
+
        uci_free_context(state->uci);
 
        free(state);
@@ -226,13 +234,13 @@ stop(struct fw3_state *state, bool complete, bool restart)
 }
 
 static int
-start(struct fw3_state *state, bool restart)
+start(struct fw3_state *state, bool reload)
 {
        int rv = 1;
        enum fw3_family family;
        enum fw3_table table;
 
-       if (!print_rules && !restart &&
+       if (!print_rules && !reload &&
            fw3_command_pipe(false, "ipset", "-exist", "-"))
        {
                fw3_create_ipsets(state);
@@ -244,7 +252,7 @@ start(struct fw3_state *state, bool restart)
                if (!family_used(family))
                        continue;
 
-               if (!print_rules && !restart && family_running(state, family))
+               if (!print_rules && !reload && family_running(state, family))
                {
                        warn("The %s firewall appears to be started already. "
                             "If it is indeed empty, remove the %s file and retry.",
@@ -277,12 +285,18 @@ start(struct fw3_state *state, bool restart)
                        fw3_pr("COMMIT\n");
                }
 
+               if (!reload)
+                       fw3_print_includes(family, state);
+
                fw3_command_close();
                family_set(state, family, true);
 
                rv = 0;
        }
 
+       if (!reload && !print_rules)
+               fw3_run_includes(state);
+
        if (!rv && !print_rules)
                fw3_write_statefile(state);
 
@@ -334,7 +348,7 @@ lookup_device(struct fw3_state *state, const char *dev)
 static int
 usage(void)
 {
-       fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
+       fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|reload|restart|print}\n");
        fprintf(stderr, "fw3 [-q] network {net}\n");
        fprintf(stderr, "fw3 [-q] device {dev}\n");
 
@@ -370,9 +384,6 @@ int main(int argc, char **argv)
                }
        }
 
-       if (!fw3_ubus_connect())
-               error("Failed to connect to ubus");
-
        state = build_state();
        defs = &state->defaults;
 
@@ -414,6 +425,14 @@ int main(int argc, char **argv)
        }
        else if (!strcmp(argv[optind], "restart"))
        {
+               stop(state, true, false);
+               free_state(state);
+
+               state = build_state();
+               rv = start(state, false);
+       }
+       else if (!strcmp(argv[optind], "reload"))
+       {
                rv = stop(state, false, true);
                rv = start(state, !rv);
        }