]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: make iptables and ip6tables use xtables_free_opts
authorJamal Hadi Salim <hadi@cyberus.ca>
Thu, 12 Feb 2009 16:43:01 +0000 (11:43 -0500)
committerJan Engelhardt <jengelh@medozas.de>
Thu, 12 Feb 2009 17:58:30 +0000 (18:58 +0100)
The patch modifies xtables_globals to introduce orig_opts and
xtables_free_opts() to emulate what free_opts used to do. We also get
rid of the copies of free_opts() that iptables and ip6tables keep.

Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
include/xtables.h.in
ip6tables.c
iptables.c
xtables.c

index 5af52071ceeba45f49e2a7c4d7b66c4643bbea45..a884343babcb0c5e48f58323328c956a85acb2eb 100644 (file)
@@ -187,6 +187,7 @@ struct xtables_globals
 {
        unsigned int option_offset;
        const char *program_name, *program_version;
+       struct option *orig_opts;
        struct option *opts;
        void (*exit_err)(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
 };
@@ -205,7 +206,7 @@ extern int xtables_insmod(const char *, const char *, bool);
 extern int xtables_load_ko(const char *, bool);
 extern int xtables_set_params(struct xtables_globals *xtp);
 extern void xtables_set_revision(char *name, u_int8_t revision);
-void xtables_free_opts(int reset_offset, struct option *original_opts);
+extern void xtables_free_opts(int reset_offset);
 
 extern struct xtables_match *xtables_find_match(const char *name,
        enum xtables_tryload, struct xtables_rule_match **match);
index 19c3258c0f0974b1c64b7c321bb904e5c8df9d1c..f7a882e60e519fdd1c82264edfae1ea06487470a 100644 (file)
@@ -150,6 +150,7 @@ struct xtables_globals ip6tables_globals = {
        .option_offset = 0,
        .program_version = IPTABLES_VERSION,
        .opts = original_opts,
+       .orig_opts = original_opts,
        .exit_err = ip6tables_exit_error,
 };
 
@@ -224,16 +225,6 @@ proto_to_name(u_int8_t proto, int nolookup)
        return NULL;
 }
 
-static void free_opts(int reset_offset)
-{
-       if (opts != original_opts) {
-               free(opts);
-               opts = original_opts;
-               if (reset_offset)
-                       global_option_offset = 0;
-       }
-}
-
 static void
 exit_tryhelp(int status)
 {
@@ -241,7 +232,7 @@ exit_tryhelp(int status)
                fprintf(stderr, "Error occurred at line: %d\n", line);
        fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
                        program_name, program_name );
-       free_opts(1);
+       xtables_free_opts(1);
        exit(status);
 }
 
@@ -351,7 +342,7 @@ ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
                fprintf(stderr,
                        "Perhaps ip6tables or your kernel needs to be upgraded.\n");
        /* On error paths, make sure that we don't leak memory */
-       free_opts(1);
+       xtables_free_opts(1);
        exit(status);
 }
 
@@ -530,7 +521,7 @@ merge_options(struct option *oldopts, const struct option *newopts,
 
        merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
        memcpy(merge, oldopts, num_old * sizeof(struct option));
-       free_opts(0); /* Release previous options merged if any */
+       xtables_free_opts(0); /* Release previous options merged if any */
        for (i = 0; i < num_new; i++) {
                merge[num_old + i] = newopts[i];
                merge[num_old + i].val += *option_offset;
@@ -2048,7 +2039,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
        for (i = 0; i < ndaddrs; i++)
                free(&daddrs[i]);
 
-       free_opts(1);
+       xtables_free_opts(1);
 
        return ret;
 }
index 7974f9aaa6df814bf6696049de5d88eb1557e9fe..fa9cc285909eed9de8412df6171b3a9330040b40 100644 (file)
@@ -151,6 +151,7 @@ struct xtables_globals iptables_globals = {
        .option_offset = 0,
        .program_version = IPTABLES_VERSION,
        .opts = original_opts,
+       .orig_opts = original_opts,
        .exit_err = iptables_exit_error,
 };
 
@@ -237,16 +238,6 @@ enum {
        IPT_DOTTED_MASK
 };
 
-static void free_opts(int reset_offset)
-{
-       if (opts != original_opts) {
-               free(opts);
-               opts = original_opts;
-               if (reset_offset)
-                       global_option_offset = 0;
-       }
-}
-
 static void
 exit_tryhelp(int status)
 {
@@ -254,7 +245,7 @@ exit_tryhelp(int status)
                fprintf(stderr, "Error occurred at line: %d\n", line);
        fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
                        program_name, program_name );
-       free_opts(1);
+       xtables_free_opts(1);
        exit(status);
 }
 
@@ -364,7 +355,7 @@ iptables_exit_error(enum xtables_exittype status, const char *msg, ...)
                fprintf(stderr,
                        "Perhaps iptables or your kernel needs to be upgraded.\n");
        /* On error paths, make sure that we don't leak memory */
-       free_opts(1);
+       xtables_free_opts(1);
        exit(status);
 }
 
@@ -536,7 +527,7 @@ merge_options(struct option *oldopts, const struct option *newopts,
        if (merge == NULL)
                return NULL;
        memcpy(merge, oldopts, num_old * sizeof(struct option));
-       free_opts(0); /* Release previous options merged if any */
+       xtables_free_opts(0); /* Release previous options merged if any */
        for (i = 0; i < num_new; i++) {
                merge[num_old + i] = newopts[i];
                merge[num_old + i].val += *option_offset;
@@ -1342,7 +1333,7 @@ get_kernel_version(void) {
 
        if (uname(&uts) == -1) {
                fprintf(stderr, "Unable to retrieve kernel version.\n");
-               free_opts(1);
+               xtables_free_opts(1);
                exit(1);
        }
 
@@ -2087,7 +2078,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
 
        free(saddrs);
        free(daddrs);
-       free_opts(1);
+       xtables_free_opts(1);
 
        return ret;
 }
index bc6a65e5bc6110f557cd97dbca65131ff3a72710..c8024bf225282cf78a5c3e3f6440ba129290acb3 100644 (file)
--- a/xtables.c
+++ b/xtables.c
@@ -97,12 +97,11 @@ int xtables_set_params(struct xtables_globals *xtp)
        return 0;
 }
 
-void xtables_free_opts(int reset_offset, struct option *original_opts)
+void xtables_free_opts(int reset_offset)
 {
-       if (xt_params->opts != original_opts) {
-               if (original_opts) 
-                       free(xt_params->opts);
-               xt_params->opts = original_opts;
+       if (xt_params->opts != xt_params->orig_opts) {
+               free(xt_params->opts);
+               xt_params->opts = xt_params->orig_opts;
                if (reset_offset)
                        xt_params->option_offset = 0;
        }