From aa37acc1423126f555135935c687eb91995b9440 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Mon, 7 Feb 2011 04:00:50 +0100 Subject: libxtables: guided option parser This patchset seeks to drastically reduce the code in the individual extensions by centralizing their argument parsing (breakdown of strings), validation, and in part, assignment. As a secondary goal, this reduces the number of static storage duration variables in flight. Signed-off-by: Jan Engelhardt --- include/xtables.h.in | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) (limited to 'include') diff --git a/include/xtables.h.in b/include/xtables.h.in index c3d34af5..928f465c 100644 --- a/include/xtables.h.in +++ b/include/xtables.h.in @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include #include @@ -34,6 +36,73 @@ struct in_addr; +/* + * .size is here so that there is a somewhat reasonable check + * against the chosen .type. + */ +#define XTOPT_POINTER(stype, member) \ + .ptroff = offsetof(stype, member), \ + .size = sizeof(((stype *)NULL)->member) +#define XTOPT_TABLEEND {.name = NULL} + +/** + * %XTTYPE_NONE: option takes no argument + */ +enum xt_option_type { + XTTYPE_NONE, +}; + +/** + * %XTOPT_INVERT: option is invertible (usable with !) + * %XTOPT_MAND: option is mandatory + * %XTOPT_MULTI: option may be specified multiple times + * %XTOPT_PUT: store value into memory at @ptroff + */ +enum xt_option_flags { + XTOPT_INVERT = 1 << 0, + XTOPT_MAND = 1 << 1, + XTOPT_MULTI = 1 << 2, + XTOPT_PUT = 1 << 3, +}; + +/** + * @name: name of option + * @type: type of input and validation method, see %XTTYPE_* + * @id: unique number (within extension) for option, 0-31 + * @excl: bitmask of flags that cannot be used with this option + * @also: bitmask of flags that must be used with this option + * @flags: bitmask of option flags, see %XTOPT_* + * @ptroff: offset into private structure for member + * @size: size of the item pointed to by @ptroff; this is a safeguard + */ +struct xt_option_entry { + const char *name; + enum xt_option_type type; + unsigned int id, excl, also, flags; + unsigned int ptroff; + size_t size; +}; + +/** + * @arg: input from command line + * @ext_name: name of extension currently being processed + * @entry: current option being processed + * @data: per-extension data block + * @xflags: options of the extension that have been used + * @invert: whether option was used with ! + * @val: parsed result + */ +struct xt_option_call { + const char *arg, *ext_name; + const struct xt_option_entry *entry; + void *data; + unsigned int xflags; + bool invert; + union { + /* to be filled */ + } val; +}; + /* Include file for additions: new matches and targets. */ struct xtables_match { @@ -86,6 +155,10 @@ struct xtables_match /* Pointer to list of extra command-line options */ const struct option *extra_opts; + /* New parser */ + void (*x6_parse)(struct xt_option_call *); + const struct xt_option_entry *x6_options; + /* Ignore these men behind the curtain: */ unsigned int option_offset; struct xt_entry_match *m; @@ -145,6 +218,10 @@ struct xtables_target /* Pointer to list of extra command-line options */ const struct option *extra_opts; + /* New parser */ + void (*x6_parse)(struct xt_option_call *); + const struct xt_option_entry *x6_options; + /* Ignore these men behind the curtain: */ unsigned int option_offset; struct xt_entry_target *t; @@ -292,6 +369,20 @@ extern void xtables_save_string(const char *value); extern const struct xtables_pprot xtables_chain_protos[]; extern u_int16_t xtables_parse_protocol(const char *s); +/* xtoptions.c */ +extern void xtables_option_metavalidate(const char *, + const struct xt_option_entry *); +extern struct option *xtables_options_xfrm(struct option *, struct option *, + const struct xt_option_entry *, + unsigned int *); +extern void xtables_option_parse(struct xt_option_call *); +extern void xtables_option_tpcall(unsigned int, char **, bool, + struct xtables_target *, void *); +extern void xtables_option_mpcall(unsigned int, char **, bool, + struct xtables_match *, void *); +extern void xtables_options_fcheck(const char *, unsigned int, + const struct xt_option_entry *); + #ifdef XTABLES_INTERNAL /* Shipped modules rely on this... */ -- cgit v1.2.3 From 3af739b0e7c3b6dcc986645c57c982d0add5006b Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Thu, 10 Feb 2011 16:57:37 +0100 Subject: libxtables: provide better final_check This passes the per-extension data block to the new x6_fcheck function pointer, which can then do last alterations without using hacks like global variables (think libxt_statistic). Signed-off-by: Jan Engelhardt --- include/xtables.h.in | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'include') diff --git a/include/xtables.h.in b/include/xtables.h.in index 928f465c..c281fed7 100644 --- a/include/xtables.h.in +++ b/include/xtables.h.in @@ -103,6 +103,17 @@ struct xt_option_call { } val; }; +/** + * @ext_name: name of extension currently being processed + * @data: per-extension data block + * @xflags: options of the extension that have been used + */ +struct xt_fcheck_call { + const char *ext_name; + void *data; + unsigned int xflags; +}; + /* Include file for additions: new matches and targets. */ struct xtables_match { @@ -157,6 +168,7 @@ struct xtables_match /* New parser */ void (*x6_parse)(struct xt_option_call *); + void (*x6_fcheck)(struct xt_fcheck_call *); const struct xt_option_entry *x6_options; /* Ignore these men behind the curtain: */ @@ -220,6 +232,7 @@ struct xtables_target /* New parser */ void (*x6_parse)(struct xt_option_call *); + void (*x6_fcheck)(struct xt_fcheck_call *); const struct xt_option_entry *x6_options; /* Ignore these men behind the curtain: */ @@ -380,6 +393,8 @@ extern void xtables_option_tpcall(unsigned int, char **, bool, struct xtables_target *, void *); extern void xtables_option_mpcall(unsigned int, char **, bool, struct xtables_match *, void *); +extern void xtables_option_tfcall(struct xtables_target *); +extern void xtables_option_mfcall(struct xtables_match *); extern void xtables_options_fcheck(const char *, unsigned int, const struct xt_option_entry *); -- cgit v1.2.3 From a93142d5f55db74ebd7d49be9bd88f7a499ded40 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Wed, 16 Feb 2011 01:22:25 +0100 Subject: libxtables: XTTYPE_UINT32 support Signed-off-by: Jan Engelhardt --- include/xtables.h.in | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/xtables.h.in b/include/xtables.h.in index c281fed7..91a6eaaa 100644 --- a/include/xtables.h.in +++ b/include/xtables.h.in @@ -47,9 +47,11 @@ struct in_addr; /** * %XTTYPE_NONE: option takes no argument + * %XTTYPE_UINT*: standard integer */ enum xt_option_type { XTTYPE_NONE, + XTTYPE_UINT32, }; /** @@ -99,7 +101,7 @@ struct xt_option_call { unsigned int xflags; bool invert; union { - /* to be filled */ + uint32_t u32; } val; }; -- cgit v1.2.3 From d78254d7f9d18ef76377a3013302430cce8ea702 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Sun, 27 Feb 2011 17:38:34 +0100 Subject: libxtables: min-max option support Signed-off-by: Jan Engelhardt --- include/xtables.h.in | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include') diff --git a/include/xtables.h.in b/include/xtables.h.in index 91a6eaaa..14d7b043 100644 --- a/include/xtables.h.in +++ b/include/xtables.h.in @@ -76,6 +76,8 @@ enum xt_option_flags { * @flags: bitmask of option flags, see %XTOPT_* * @ptroff: offset into private structure for member * @size: size of the item pointed to by @ptroff; this is a safeguard + * @min: lowest allowed value (for singular integral types) + * @max: highest allowed value (for singular integral types) */ struct xt_option_entry { const char *name; @@ -83,6 +85,7 @@ struct xt_option_entry { unsigned int id, excl, also, flags; unsigned int ptroff; size_t size; + unsigned int min, max; }; /** -- cgit v1.2.3