]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: consolidate merge_options into xtables_merge_options
authorJamal Hadi Salim <hadi@cyberus.ca>
Fri, 13 Feb 2009 13:36:44 +0000 (08:36 -0500)
committerJan Engelhardt <jengelh@medozas.de>
Fri, 13 Feb 2009 13:57:01 +0000 (14:57 +0100)
Introduce xtables_merge_options() for re-use reasons. Apps can use it
instead of each defining their own merge_options(). Made iptables and
ip6tables use the new shared interface.

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

index a884343babcb0c5e48f58323328c956a85acb2eb..aaadbbd65072a2aef59e92f856a02ef852efae20 100644 (file)
@@ -207,6 +207,8 @@ 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);
 extern void xtables_free_opts(int reset_offset);
+extern struct option *xtables_merge_options(struct option *oldopts,
+       const struct option *newopts, unsigned int *option_offset);
 
 extern struct xtables_match *xtables_find_match(const char *name,
        enum xtables_tryload, struct xtables_rule_match **match);
@@ -233,6 +235,8 @@ int xtables_check_inverse(const char option[], int *invert,
        int *my_optind, int argc);
 extern struct xtables_globals *xt_params;
 #define exit_error xt_params->exit_err
+#define OPTION_OFFSET 256
+
 extern void xtables_param_act(unsigned int, const char *, ...);
 
 extern const char *xtables_ipaddr_to_numeric(const struct in_addr *);
index f7a882e60e519fdd1c82264edfae1ea06487470a..250bb0ea5fd3df72fdf53ad8d1c353c24d62ed39 100644 (file)
@@ -84,8 +84,6 @@
 static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
                                 'N', 'X', 'P', 'E', 'S' };
 
-#define OPTION_OFFSET 256
-
 #define OPT_NONE       0x00000U
 #define OPT_NUMERIC    0x00001U
 #define OPT_SOURCE     0x00002U
@@ -144,7 +142,6 @@ static struct option original_opts[] = {
 int line = -1;
 
 static struct option *opts = original_opts;
-static unsigned int global_option_offset = 0;
 void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
 struct xtables_globals ip6tables_globals = {
        .option_offset = 0,
@@ -503,34 +500,6 @@ set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
        }
 }
 
-static struct option *
-merge_options(struct option *oldopts, const struct option *newopts,
-             unsigned int *option_offset)
-{
-       unsigned int num_old, num_new, i;
-       struct option *merge;
-
-       if (newopts == NULL)
-               return oldopts;
-
-       for (num_old = 0; oldopts[num_old].name; num_old++);
-       for (num_new = 0; newopts[num_new].name; num_new++);
-
-       global_option_offset += OPTION_OFFSET;
-       *option_offset = global_option_offset;
-
-       merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
-       memcpy(merge, oldopts, num_old * sizeof(struct option));
-       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;
-       }
-       memset(merge + num_old + num_new, 0, sizeof(struct option));
-
-       return merge;
-}
-
 static void
 print_num(u_int64_t number, unsigned int format)
 {
@@ -1584,7 +1553,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
                                             target->revision);
                                if (target->init != NULL)
                                        target->init(target->t);
-                               opts = merge_options(opts,
+                               opts = xtables_merge_options(opts,
                                                     target->extra_opts,
                                                     &target->option_offset);
                                if (opts == NULL)
@@ -1638,7 +1607,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
                                m->init(m->m);
                        if (m != m->next)
                                /* Merge options for non-cloned matches */
-                               opts = merge_options(opts, m->extra_opts, &m->option_offset);
+                               opts = xtables_merge_options(opts, m->extra_opts, &m->option_offset);
                }
                break;
 
@@ -1785,7 +1754,7 @@ int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **hand
                                        if (m->init != NULL)
                                                m->init(m->m);
 
-                                       opts = merge_options(opts,
+                                       opts = xtables_merge_options(opts,
                                            m->extra_opts, &m->option_offset);
 
                                        optind--;
index fa9cc285909eed9de8412df6171b3a9330040b40..e6b4aa04c31cbde042aa4bffb43355e4fad70e06 100644 (file)
@@ -81,8 +81,6 @@
 static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
                                 'N', 'X', 'P', 'E', 'S' };
 
-#define OPTION_OFFSET 256
-
 #define OPT_NONE       0x00000U
 #define OPT_NUMERIC    0x00001U
 #define OPT_SOURCE     0x00002U
@@ -143,7 +141,6 @@ static struct option original_opts[] = {
 int line = -1;
 
 static struct option *opts = original_opts;
-static unsigned int global_option_offset = 0;
 
 void iptables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
 
@@ -507,36 +504,6 @@ set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
        }
 }
 
-static struct option *
-merge_options(struct option *oldopts, const struct option *newopts,
-             unsigned int *option_offset)
-{
-       unsigned int num_old, num_new, i;
-       struct option *merge;
-
-       if (newopts == NULL)
-               return oldopts;
-
-       for (num_old = 0; oldopts[num_old].name; num_old++);
-       for (num_new = 0; newopts[num_new].name; num_new++);
-
-       global_option_offset += OPTION_OFFSET;
-       *option_offset = global_option_offset;
-
-       merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
-       if (merge == NULL)
-               return NULL;
-       memcpy(merge, oldopts, num_old * sizeof(struct option));
-       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;
-       }
-       memset(merge + num_old + num_new, 0, sizeof(struct option));
-
-       return merge;
-}
-
 static void
 print_num(u_int64_t number, unsigned int format)
 {
@@ -1600,7 +1567,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
                                             target->revision);
                                if (target->init != NULL)
                                        target->init(target->t);
-                               opts = merge_options(opts,
+                               opts = xtables_merge_options(opts,
                                                     target->extra_opts,
                                                     &target->option_offset);
                                if (opts == NULL)
@@ -1660,7 +1627,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
                                m->init(m->m);
                        if (m != m->next) {
                                /* Merge options for non-cloned matches */
-                               opts = merge_options(opts,
+                               opts = xtables_merge_options(opts,
                                                     m->extra_opts,
                                                     &m->option_offset);
                                if (opts == NULL)
@@ -1814,7 +1781,7 @@ int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle
                                        if (m->init != NULL)
                                                m->init(m->m);
 
-                                       opts = merge_options(opts,
+                                       opts = xtables_merge_options(opts,
                                                             m->extra_opts,
                                                             &m->option_offset);
                                        if (opts == NULL)
index c8024bf225282cf78a5c3e3f6440ba129290acb3..bdb0133dd121dc84d15b0fa913392570935ddbe9 100644 (file)
--- a/xtables.c
+++ b/xtables.c
@@ -47,6 +47,7 @@
 #      define IP6T_SO_GET_REVISION_MATCH       68
 #      define IP6T_SO_GET_REVISION_TARGET      69
 #endif
+#include <getopt.h>
 
 
 #define NPROTO 255
@@ -107,6 +108,36 @@ void xtables_free_opts(int reset_offset)
        }
 }
 
+struct option *xtables_merge_options(struct option *oldopts,
+                                    const struct option *newopts,
+                                    unsigned int *option_offset)
+{
+       unsigned int num_old, num_new, i;
+       struct option *merge;
+
+       if (newopts == NULL)
+               return oldopts;
+
+       for (num_old = 0; oldopts[num_old].name; num_old++) ;
+       for (num_new = 0; newopts[num_new].name; num_new++) ;
+
+       xt_params->option_offset += OPTION_OFFSET;
+       *option_offset = xt_params->option_offset;
+
+       merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
+       if (merge == NULL)
+               return NULL;
+       memcpy(merge, oldopts, num_old * sizeof(struct option));
+       xtables_free_opts(0);   /* Release any old options merged  */
+       for (i = 0; i < num_new; i++) {
+               merge[num_old + i] = newopts[i];
+               merge[num_old + i].val += *option_offset;
+       }
+       memset(merge + num_old + num_new, 0, sizeof(struct option));
+
+       return merge;
+}
+
 void xtables_set_revision(char *name, u_int8_t revision)
 {
        /* Old kernel sources don't have ".revision" field,