]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxt_NFQUEUE: use guided option parser
authorJan Engelhardt <jengelh@medozas.de>
Sun, 6 Mar 2011 16:54:50 +0000 (17:54 +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_NFQUEUE.c

index 7f2aab16f6ac9d70cf495fb624e3b2896254763d..a86c88a3cdade0f21b46eec33579521fc9cccaf8 100644 (file)
@@ -5,16 +5,16 @@
  * This program is distributed under the terms of GNU GPL v2, 1991
  *
  */
-#include <stdbool.h>
 #include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <getopt.h>
-
 #include <xtables.h>
-#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter/xt_NFQUEUE.h>
 
+enum {
+       O_QUEUE_NUM = 0,
+       O_QUEUE_BALANCE,
+       O_QUEUE_BYPASS,
+};
+
 static void NFQUEUE_help(void)
 {
        printf(
@@ -38,103 +38,58 @@ static void NFQUEUE_help_v2(void)
 "  --queue-bypass              Bypass Queueing if no queue instance exists.\n");
 }
 
-static const struct option NFQUEUE_opts[] = {
-       {.name = "queue-num",     .has_arg = true, .val = 'F'},
-       {.name = "queue-balance", .has_arg = true, .val = 'B'},
-       {.name = "queue-bypass",  .has_arg = false,.val = 'P'},
-       XT_GETOPT_TABLEEND,
+#define s struct xt_NFQ_info
+static const struct xt_option_entry NFQUEUE_opts[] = {
+       {.name = "queue-num", .id = O_QUEUE_NUM, .type = XTTYPE_UINT16,
+        .flags = XTOPT_PUT, XTOPT_POINTER(s, queuenum)},
+       {.name = "queue-balance", .id = O_QUEUE_BALANCE,
+        .type = XTTYPE_UINT16RC},
+       {.name = "queue-bypass", .id = O_QUEUE_BYPASS, .type = XTTYPE_NONE},
+       XTOPT_TABLEEND,
 };
+#undef s
 
-static void exit_badqueue(const char *s)
-{
-       xtables_error(PARAMETER_PROBLEM, "Invalid queue number `%s'\n", s);
-}
-
-static void
-parse_num(const char *s, struct xt_NFQ_info *tinfo)
+static void NFQUEUE_parse(struct xt_option_call *cb)
 {
-       unsigned int num;
-
-       if (!xtables_strtoui(s, NULL, &num, 0, UINT16_MAX))
-               exit_badqueue(s);
-
-       tinfo->queuenum = num;
-}
-
-static int
-NFQUEUE_parse(int c, char **argv, int invert, unsigned int *flags,
-              const void *entry, struct xt_entry_target **target)
-{
-       struct xt_NFQ_info *tinfo
-               = (struct xt_NFQ_info *)(*target)->data;
-
-       switch (c) {
-       case 'F':
-               if (*flags)
-                       xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
-                                  "Only use --queue-num ONCE!");
-               parse_num(optarg, tinfo);
-               break;
-       case 'B':
+       xtables_option_parse(cb);
+       if (cb->entry->id == O_QUEUE_BALANCE)
                xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
                                   "--queue-balance not supported (kernel too old?)");
-       }
-
-       return 1;
 }
 
-static int
-NFQUEUE_parse_v1(int c, char **argv, int invert, unsigned int *flags,
-                 const void *entry, struct xt_entry_target **target)
+static void NFQUEUE_parse_v1(struct xt_option_call *cb)
 {
-       struct xt_NFQ_info_v1 *info = (void *)(*target)->data;
-       char *colon;
-       unsigned int firstqueue, lastqueue;
-
-       switch (c) {
-       case 'F': /* fallthrough */
-       case 'B':
-               if (*flags)
-                       xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
-                                  "Only use --queue-num ONCE!");
-
-               if (!xtables_strtoui(optarg, &colon, &firstqueue, 0, UINT16_MAX))
-                       exit_badqueue(optarg);
-
-               info->queuenum = firstqueue;
-
-               if (c == 'F') {
-                       if (*colon)
-                               exit_badqueue(optarg);
-                       break;
-               }
+       struct xt_NFQ_info_v1 *info = cb->data;
+       const uint16_t *r = cb->val.u16_range;
 
-               if (*colon != ':')
-                       xtables_error(PARAMETER_PROBLEM, "Bad range \"%s\"", optarg);
-
-               if (!xtables_strtoui(colon + 1, NULL, &lastqueue, 1, UINT16_MAX))
-                       exit_badqueue(optarg);
-
-               if (firstqueue >= lastqueue)
+       xtables_option_parse(cb);
+       switch (cb->entry->id) {
+       case O_QUEUE_BALANCE:
+               if (cb->nvals != 2)
+                       xtables_error(PARAMETER_PROBLEM,
+                               "Bad range \"%s\"", cb->arg);
+               if (r[0] >= r[1])
                        xtables_error(PARAMETER_PROBLEM, "%u should be less than %u",
-                                                       firstqueue, lastqueue);
-               info->queues_total = lastqueue - firstqueue + 1;
+                               r[0], r[1]);
+               info->queuenum = r[0];
+               info->queues_total = r[1] - r[0] + 1;
                break;
        }
-
-       return 1;
 }
 
-static int
-NFQUEUE_parse_v2(int c, char **argv, int invert, unsigned int *flags,
-                 const void *entry, struct xt_entry_target **target)
+static void NFQUEUE_parse_v2(struct xt_option_call *cb)
 {
-       if (c == 'P') {
-               struct xt_NFQ_info_v2 *info = (void *)(*target)->data;
+       struct xt_NFQ_info_v2 *info = cb->data;
+
+       xtables_option_parse(cb);
+       switch (cb->entry->id) {
+       case O_QUEUE_BYPASS:
                info->bypass = 1;
-               return 1;
+               break;
+       default:
+               NFQUEUE_parse_v1(cb);
+               break;
        }
-       return NFQUEUE_parse_v1(c, argv, invert, flags, entry, target);
 }
 
 static void NFQUEUE_print(const void *ip,
@@ -214,10 +169,10 @@ static struct xtables_target nfqueue_targets[] = {
        .size           = XT_ALIGN(sizeof(struct xt_NFQ_info)),
        .userspacesize  = XT_ALIGN(sizeof(struct xt_NFQ_info)),
        .help           = NFQUEUE_help,
-       .parse          = NFQUEUE_parse,
        .print          = NFQUEUE_print,
        .save           = NFQUEUE_save,
-       .extra_opts     = NFQUEUE_opts
+       .x6_parse       = NFQUEUE_parse,
+       .x6_options     = NFQUEUE_opts
 },{
        .family         = NFPROTO_UNSPEC,
        .revision       = 1,
@@ -227,10 +182,10 @@ static struct xtables_target nfqueue_targets[] = {
        .userspacesize  = XT_ALIGN(sizeof(struct xt_NFQ_info_v1)),
        .help           = NFQUEUE_help_v1,
        .init           = NFQUEUE_init_v1,
-       .parse          = NFQUEUE_parse_v1,
        .print          = NFQUEUE_print_v1,
        .save           = NFQUEUE_save_v1,
-       .extra_opts     = NFQUEUE_opts,
+       .x6_parse       = NFQUEUE_parse_v1,
+       .x6_options     = NFQUEUE_opts,
 },{
        .family         = NFPROTO_UNSPEC,
        .revision       = 2,
@@ -240,10 +195,10 @@ static struct xtables_target nfqueue_targets[] = {
        .userspacesize  = XT_ALIGN(sizeof(struct xt_NFQ_info_v2)),
        .help           = NFQUEUE_help_v2,
        .init           = NFQUEUE_init_v1,
-       .parse          = NFQUEUE_parse_v2,
        .print          = NFQUEUE_print_v2,
        .save           = NFQUEUE_save_v2,
-       .extra_opts     = NFQUEUE_opts,
+       .x6_parse       = NFQUEUE_parse_v2,
+       .x6_options     = NFQUEUE_opts,
 }
 };