diff options
Diffstat (limited to 'extensions/libxt_length.c')
-rw-r--r-- | extensions/libxt_length.c | 108 |
1 files changed, 25 insertions, 83 deletions
diff --git a/extensions/libxt_length.c b/extensions/libxt_length.c index 96e8b6c..6ea7646 100644 --- a/extensions/libxt_length.c +++ b/extensions/libxt_length.c @@ -1,13 +1,11 @@ -/* Shared library add-on to iptables to add packet length matching support. */ #include <stdio.h> -#include <netdb.h> -#include <string.h> -#include <stdlib.h> -#include <getopt.h> - #include <xtables.h> #include <linux/netfilter/xt_length.h> +enum { + O_LENGTH = 0, +}; + static void length_help(void) { printf( @@ -16,78 +14,23 @@ static void length_help(void) " of values (inclusive)\n"); } -static const struct option length_opts[] = { - { "length", 1, NULL, '1' }, - { .name = NULL } +static const struct xt_option_entry length_opts[] = { + {.name = "length", .id = O_LENGTH, .type = XTTYPE_UINT16RC, + .flags = XTOPT_MAND | XTOPT_INVERT}, + XTOPT_TABLEEND, }; -static u_int16_t -parse_length(const char *s) -{ - unsigned int len; - - if (!xtables_strtoui(s, NULL, &len, 0, UINT32_MAX)) - xtables_error(PARAMETER_PROBLEM, "length invalid: \"%s\"\n", s); - else - return len; -} - -/* If a single value is provided, min and max are both set to the value */ -static void -parse_lengths(const char *s, struct xt_length_info *info) -{ - char *buffer; - char *cp; - - buffer = strdup(s); - if ((cp = strchr(buffer, ':')) == NULL) - info->min = info->max = parse_length(buffer); - else { - *cp = '\0'; - cp++; - - info->min = buffer[0] ? parse_length(buffer) : 0; - info->max = cp[0] ? parse_length(cp) : 0xFFFF; - } - free(buffer); - - if (info->min > info->max) - xtables_error(PARAMETER_PROBLEM, - "length min. range value `%u' greater than max. " - "range value `%u'", info->min, info->max); - -} - -static int -length_parse(int c, char **argv, int invert, unsigned int *flags, - const void *entry, struct xt_entry_match **match) +static void length_parse(struct xt_option_call *cb) { - struct xt_length_info *info = (struct xt_length_info *)(*match)->data; - - switch (c) { - case '1': - if (*flags) - xtables_error(PARAMETER_PROBLEM, - "length: `--length' may only be " - "specified once"); - xtables_check_inverse(optarg, &invert, &optind, 0, argv); - parse_lengths(optarg, info); - if (invert) - info->invert = 1; - *flags = 1; - break; - - default: - return 0; - } - return 1; -} + struct xt_length_info *info = cb->data; -static void length_check(unsigned int flags) -{ - if (!flags) - xtables_error(PARAMETER_PROBLEM, - "length: You must specify `--length'"); + xtables_option_parse(cb); + info->min = cb->val.u16_range[0]; + info->max = cb->val.u16_range[0]; + if (cb->nvals >= 2) + info->max = cb->val.u16_range[1]; + if (cb->invert) + info->invert = 1; } static void @@ -95,22 +38,22 @@ length_print(const void *ip, const struct xt_entry_match *match, int numeric) { const struct xt_length_info *info = (void *)match->data; - printf("length %s", info->invert ? "!" : ""); + printf(" length %s", info->invert ? "!" : ""); if (info->min == info->max) - printf("%u ", info->min); + printf("%u", info->min); else - printf("%u:%u ", info->min, info->max); + printf("%u:%u", info->min, info->max); } static void length_save(const void *ip, const struct xt_entry_match *match) { const struct xt_length_info *info = (void *)match->data; - printf("%s--length ", info->invert ? "! " : ""); + printf("%s --length ", info->invert ? " !" : ""); if (info->min == info->max) - printf("%u ", info->min); + printf("%u", info->min); else - printf("%u:%u ", info->min, info->max); + printf("%u:%u", info->min, info->max); } static struct xtables_match length_match = { @@ -120,11 +63,10 @@ static struct xtables_match length_match = { .size = XT_ALIGN(sizeof(struct xt_length_info)), .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)), .help = length_help, - .parse = length_parse, - .final_check = length_check, .print = length_print, .save = length_save, - .extra_opts = length_opts, + .x6_parse = length_parse, + .x6_options = length_opts, }; void _init(void) |