From 72c359784a03b1ea46a9964e5c1f8636a52507dd Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Tue, 1 Mar 2011 20:28:24 +0100 Subject: libxt_DSCP: use guided option parser Signed-off-by: Jan Engelhardt --- extensions/libxt_dscp.c | 94 +++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 66 deletions(-) (limited to 'extensions/libxt_dscp.c') diff --git a/extensions/libxt_dscp.c b/extensions/libxt_dscp.c index b07f83b0..69533d6b 100644 --- a/extensions/libxt_dscp.c +++ b/extensions/libxt_dscp.c @@ -12,19 +12,21 @@ * http://www.iana.org/assignments/dscp-registry * */ -#include #include #include -#include -#include - #include -#include #include /* This is evil, but it's my code - HW*/ #include "dscp_helper.c" +enum { + O_DSCP = 0, + O_DSCP_CLASS, + F_DSCP = 1 << O_DSCP, + F_DSCP_CLASS = 1 << O_DSCP_CLASS, +}; + static void dscp_help(void) { printf( @@ -38,76 +40,36 @@ static void dscp_help(void) " These two options are mutually exclusive !\n"); } -static const struct option dscp_opts[] = { - {.name = "dscp", .has_arg = true, .val = 'F'}, - {.name = "dscp-class", .has_arg = true, .val = 'G'}, - XT_GETOPT_TABLEEND, +static const struct xt_option_entry dscp_opts[] = { + {.name = "dscp", .id = O_DSCP, .excl = F_DSCP_CLASS, + .type = XTTYPE_UINT8, .min = 0, .max = XT_DSCP_MAX, + .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_dscp_info, dscp)}, + {.name = "dscp-class", .id = O_DSCP_CLASS, .excl = F_DSCP, + .type = XTTYPE_STRING}, + XTOPT_TABLEEND, }; -static void -parse_dscp(const char *s, struct xt_dscp_info *dinfo) -{ - unsigned int dscp; - - if (!xtables_strtoui(s, NULL, &dscp, 0, UINT8_MAX)) - xtables_error(PARAMETER_PROBLEM, - "Invalid dscp `%s'\n", s); - - if (dscp > XT_DSCP_MAX) - xtables_error(PARAMETER_PROBLEM, - "DSCP `%d` out of range\n", dscp); - - dinfo->dscp = dscp; -} - - -static void -parse_class(const char *s, struct xt_dscp_info *dinfo) -{ - unsigned int dscp = class_to_dscp(s); - - /* Assign the value */ - dinfo->dscp = dscp; -} - - -static int -dscp_parse(int c, char **argv, int invert, unsigned int *flags, - const void *entry, struct xt_entry_match **match) +static void dscp_parse(struct xt_option_call *cb) { - struct xt_dscp_info *dinfo - = (struct xt_dscp_info *)(*match)->data; + struct xt_dscp_info *dinfo = cb->data; - switch (c) { - case 'F': - if (*flags) - xtables_error(PARAMETER_PROBLEM, - "DSCP match: Only use --dscp ONCE!"); - xtables_check_inverse(optarg, &invert, &optind, 0, argv); - parse_dscp(optarg, dinfo); - if (invert) + xtables_option_parse(cb); + switch (cb->entry->id) { + case O_DSCP: + if (cb->invert) dinfo->invert = 1; - *flags = 1; break; - - case 'G': - if (*flags) - xtables_error(PARAMETER_PROBLEM, - "DSCP match: Only use --dscp-class ONCE!"); - xtables_check_inverse(optarg, &invert, &optind, 0, argv); - parse_class(optarg, dinfo); - if (invert) + case O_DSCP_CLASS: + dinfo->dscp = class_to_dscp(cb->arg); + if (cb->invert) dinfo->invert = 1; - *flags = 1; break; } - - return 1; } -static void dscp_check(unsigned int flags) +static void dscp_check(struct xt_fcheck_call *cb) { - if (!flags) + if (cb->xflags == 0) xtables_error(PARAMETER_PROBLEM, "DSCP match: Parameter --dscp is required"); } @@ -135,11 +97,11 @@ static struct xtables_match dscp_match = { .size = XT_ALIGN(sizeof(struct xt_dscp_info)), .userspacesize = XT_ALIGN(sizeof(struct xt_dscp_info)), .help = dscp_help, - .parse = dscp_parse, - .final_check = dscp_check, .print = dscp_print, .save = dscp_save, - .extra_opts = dscp_opts, + .x6_parse = dscp_parse, + .x6_fcheck = dscp_check, + .x6_options = dscp_opts, }; void _init(void) -- cgit v1.2.3