]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxt_TCPMSS: use guided option parser
authorJan Engelhardt <jengelh@medozas.de>
Sun, 6 Mar 2011 17:00:05 +0000 (18:00 +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_TCPMSS.c

index e15e87a8ba22bb19036869aa4ebd3bba16f9313e..2266326d3e214027004215244c4e6c5b5f0288fc 100644 (file)
@@ -2,16 +2,17 @@
  *
  * Copyright (c) 2000 Marc Boucher
 */
-#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 <netinet/ip.h>
+#include <netinet/ip6.h>
 #include <linux/netfilter/xt_TCPMSS.h>
 
+enum {
+       O_SET_MSS = 0,
+       O_CLAMP_MSS,
+};
+
 struct mssinfo {
        struct xt_entry_target t;
        struct xt_tcpmss_info mss;
@@ -28,69 +29,42 @@ hdrsize);
 
 static void TCPMSS_help(void)
 {
-       __TCPMSS_help(40);
+       __TCPMSS_help(sizeof(struct iphdr));
 }
 
 static void TCPMSS_help6(void)
 {
-       __TCPMSS_help(60);
+       __TCPMSS_help(sizeof(struct ip6_hdr));
 }
 
-static const struct option TCPMSS_opts[] = {
-       {.name = "set-mss",           .has_arg = true,  .val = '1'},
-       {.name = "clamp-mss-to-pmtu", .has_arg = false, .val = '2'},
-       XT_GETOPT_TABLEEND,
+static const struct xt_option_entry TCPMSS4_opts[] = {
+       {.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
+        .min = 0, .max = UINT16_MAX - sizeof(struct iphdr),
+        .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
+       {.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
+       XTOPT_TABLEEND,
 };
 
-static int __TCPMSS_parse(int c, char **argv, int invert, unsigned int *flags,
-                          const void *entry, struct xt_entry_target **target,
-                          int hdrsize)
-{
-       struct xt_tcpmss_info *mssinfo
-               = (struct xt_tcpmss_info *)(*target)->data;
-
-       switch (c) {
-               unsigned int mssval;
-
-       case '1':
-               if (*flags)
-                       xtables_error(PARAMETER_PROBLEM,
-                                  "TCPMSS target: Only one option may be specified");
-               if (!xtables_strtoui(optarg, NULL, &mssval,
-                   0, UINT16_MAX - hdrsize))
-                       xtables_error(PARAMETER_PROBLEM, "Bad TCPMSS value \"%s\"", optarg);
-               
-               mssinfo->mss = mssval;
-               *flags = 1;
-               break;
-
-       case '2':
-               if (*flags)
-                       xtables_error(PARAMETER_PROBLEM,
-                                  "TCPMSS target: Only one option may be specified");
-               mssinfo->mss = XT_TCPMSS_CLAMP_PMTU;
-               *flags = 1;
-               break;
-       }
-
-       return 1;
-}
+static const struct xt_option_entry TCPMSS6_opts[] = {
+       {.name = "set-mss", .id = O_SET_MSS, .type = XTTYPE_UINT16,
+        .min = 0, .max = UINT16_MAX - sizeof(struct ip6_hdr),
+        .flags = XTOPT_PUT, XTOPT_POINTER(struct xt_tcpmss_info, mss)},
+       {.name = "clamp-mss-to-pmtu", .id = O_CLAMP_MSS, .type = XTTYPE_NONE},
+       XTOPT_TABLEEND,
+};
 
-static int TCPMSS_parse(int c, char **argv, int invert, unsigned int *flags,
-                        const void *entry, struct xt_entry_target **target)
+static void TCPMSS_parse(struct xt_option_call *cb)
 {
-       return __TCPMSS_parse(c, argv, invert, flags, entry, target, 40);
-}
+       struct xt_tcpmss_info *mssinfo = cb->data;
 
-static int TCPMSS_parse6(int c, char **argv, int invert, unsigned int *flags,
-                         const void *entry, struct xt_entry_target **target)
-{
-       return __TCPMSS_parse(c, argv, invert, flags, entry, target, 60);
+       xtables_option_parse(cb);
+       if (cb->entry->id == O_CLAMP_MSS)
+               mssinfo->mss = XT_TCPMSS_CLAMP_PMTU;
 }
 
-static void TCPMSS_check(unsigned int flags)
+static void TCPMSS_check(struct xt_fcheck_call *cb)
 {
-       if (!flags)
+       if (cb->xflags == 0)
                xtables_error(PARAMETER_PROBLEM,
                           "TCPMSS target: At least one parameter is required");
 }
@@ -124,11 +98,11 @@ static struct xtables_target tcpmss_target = {
        .size           = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
        .userspacesize  = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
        .help           = TCPMSS_help,
-       .parse          = TCPMSS_parse,
-       .final_check    = TCPMSS_check,
        .print          = TCPMSS_print,
        .save           = TCPMSS_save,
-       .extra_opts     = TCPMSS_opts,
+       .x6_parse       = TCPMSS_parse,
+       .x6_fcheck      = TCPMSS_check,
+       .x6_options     = TCPMSS4_opts,
 };
 
 static struct xtables_target tcpmss_target6 = {
@@ -138,11 +112,11 @@ static struct xtables_target tcpmss_target6 = {
        .size           = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
        .userspacesize  = XT_ALIGN(sizeof(struct xt_tcpmss_info)),
        .help           = TCPMSS_help6,
-       .parse          = TCPMSS_parse6,
-       .final_check    = TCPMSS_check,
        .print          = TCPMSS_print,
        .save           = TCPMSS_save,
-       .extra_opts     = TCPMSS_opts,
+       .x6_parse       = TCPMSS_parse,
+       .x6_fcheck      = TCPMSS_check,
+       .x6_options     = TCPMSS6_opts,
 };
 
 void _init(void)