]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxt_DSCP: use guided option parser
authorJan Engelhardt <jengelh@medozas.de>
Tue, 1 Mar 2011 19:28:24 +0000 (20:28 +0100)
committerJan Engelhardt <jengelh@medozas.de>
Wed, 6 Apr 2011 11:12:58 +0000 (13:12 +0200)
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
extensions/libxt_DSCP.c
extensions/libxt_dscp.c

index db27d68f0b0ce6b5bed414b2b20884d7fb5d277a..e16e93c4492af216d1ab9d50bbb5f88e9084049e 100644 (file)
@@ -9,19 +9,21 @@
  *
  * --set-class added by Iain Barnes
  */
-#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_DSCP.h>
 
 /* This is evil, but it's my code - HW*/
 #include "dscp_helper.c"
 
+enum {
+       O_SET_DSCP = 0,
+       O_SET_DSCP_CLASS,
+       F_SET_DSCP       = 1 << O_SET_DSCP,
+       F_SET_DSCP_CLASS = 1 << O_SET_DSCP_CLASS,
+};
+
 static void DSCP_help(void)
 {
        printf(
@@ -38,68 +40,31 @@ static void DSCP_help(void)
 );
 }
 
-static const struct option DSCP_opts[] = {
-       {.name = "set-dscp",       .has_arg = true, .val = 'F'},
-       {.name = "set-dscp-class", .has_arg = true, .val = 'G'},
-       XT_GETOPT_TABLEEND,
+static const struct xt_option_entry DSCP_opts[] = {
+       {.name = "set-dscp", .id = O_SET_DSCP, .excl = F_SET_DSCP_CLASS,
+        .type = XTTYPE_UINT8, .min = 0, .max = XT_DSCP_MAX,
+        .flags = XTOPT_PUT,
+        XTOPT_POINTER(struct xt_DSCP_info, dscp)},
+       {.name = "set-dscp-class", .id = O_SET_DSCP_CLASS, .excl = F_SET_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_target **target)
+static void DSCP_parse(struct xt_option_call *cb)
 {
-       struct xt_DSCP_info *dinfo
-               = (struct xt_DSCP_info *)(*target)->data;
+       struct xt_DSCP_info *dinfo = cb->data;
 
-       switch (c) {
-       case 'F':
-               if (*flags)
-                       xtables_error(PARAMETER_PROBLEM,
-                                  "DSCP target: Only use --set-dscp ONCE!");
-               parse_dscp(optarg, dinfo);
-               *flags = 1;
-               break;
-       case 'G':
-               if (*flags)
-                       xtables_error(PARAMETER_PROBLEM,
-                                  "DSCP target: Only use --set-dscp-class ONCE!");
-               parse_class(optarg, dinfo);
-               *flags = 1;
+       xtables_option_parse(cb);
+       switch (cb->entry->id) {
+       case O_SET_DSCP_CLASS:
+               dinfo->dscp = class_to_dscp(cb->arg);
                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 target: Parameter --set-dscp is required");
 }
@@ -134,11 +99,11 @@ static struct xtables_target dscp_target = {
        .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)
index b07f83b00054077660ec25dbc53fb1860d7f4ce4..69533d6b22c6e29e01f8e425fefc3b3c060eace0 100644 (file)
  * http://www.iana.org/assignments/dscp-registry
  *
  */
-#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_dscp.h>
 
 /* 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)