]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxt_quota: use guided option parser
authorJan Engelhardt <jengelh@medozas.de>
Sun, 6 Mar 2011 15:58:24 +0000 (16:58 +0100)
committerJan Engelhardt <jengelh@medozas.de>
Wed, 13 Apr 2011 16:09:26 +0000 (18:09 +0200)
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
extensions/libxt_quota.c

index e3699ad788240ada06f31102bdf8e0f39757d461..988f404f5f128cac4f24dfa752d7591bee04bd51 100644 (file)
@@ -3,18 +3,18 @@
  *
  * Sam Johnston <samj@samj.net>
  */
-#include <stdbool.h>
-#include <stddef.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <getopt.h>
 #include <xtables.h>
-
 #include <linux/netfilter/xt_quota.h>
 
-static const struct option quota_opts[] = {
-       {.name = "quota", .has_arg = true, .val = '1'},
-       XT_GETOPT_TABLEEND,
+enum {
+       O_QUOTA = 0,
+};
+
+static const struct xt_option_entry quota_opts[] = {
+       {.name = "quota", .id = O_QUOTA, .type = XTTYPE_UINT64,
+        .flags = XTOPT_MAND | XTOPT_INVERT},
+       XTOPT_TABLEEND,
 };
 
 static void quota_help(void)
@@ -40,49 +40,13 @@ quota_save(const void *ip, const struct xt_entry_match *match)
        printf(" --quota %llu", (unsigned long long) q->quota);
 }
 
-/* parse quota option */
-static int
-parse_quota(const char *s, uint64_t * quota)
-{
-       *quota = strtoull(s, NULL, 10);
-
-#ifdef DEBUG_XT_QUOTA
-       printf("Quota: %llu\n", *quota);
-#endif
-
-       if (*quota == UINT64_MAX)
-               xtables_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
-       else
-               return 1;
-}
-
-static int
-quota_parse(int c, char **argv, int invert, unsigned int *flags,
-           const void *entry, struct xt_entry_match **match)
+static void quota_parse(struct xt_option_call *cb)
 {
-       struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
-
-       switch (c) {
-       case '1':
-               if (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
-                       xtables_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
-               if (!parse_quota(optarg, &info->quota))
-                       xtables_error(PARAMETER_PROBLEM,
-                                  "bad quota: '%s'", optarg);
+       struct xt_quota_info *info = cb->data;
 
-               if (invert)
-                       info->flags |= XT_QUOTA_INVERT;
-               *flags |= 1;
-               break;
-       }
-       return 1;
-}
-
-static void quota_check(unsigned int flags)
-{
-       if (flags == 0)
-               xtables_error(PARAMETER_PROBLEM,
-                       "quota: the --quota argument must be specified\n");
+       xtables_option_parse(cb);
+       if (cb->invert)
+               info->flags |= XT_QUOTA_INVERT;
 }
 
 static struct xtables_match quota_match = {
@@ -92,11 +56,10 @@ static struct xtables_match quota_match = {
        .size           = XT_ALIGN(sizeof (struct xt_quota_info)),
        .userspacesize  = offsetof(struct xt_quota_info, master),
        .help           = quota_help,
-       .parse          = quota_parse,
-       .final_check    = quota_check,
        .print          = quota_print,
        .save           = quota_save,
-       .extra_opts     = quota_opts,
+       .x6_parse       = quota_parse,
+       .x6_options     = quota_opts,
 };
 
 void