netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan Engelhardt <jengelh@medozas.de>
To: kaber@trash.net
Cc: netfilter-devel@vger.kernel.org
Subject: [PATCH 12/13] libxt_rateest: use guided option parser
Date: Mon, 23 May 2011 16:39:22 +0200	[thread overview]
Message-ID: <1306161564-4370-13-git-send-email-jengelh@medozas.de> (raw)
In-Reply-To: <1306161564-4370-1-git-send-email-jengelh@medozas.de>

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
---
 extensions/libxt_rateest.c |  256 +++++++++++++++-----------------------------
 1 files changed, 88 insertions(+), 168 deletions(-)

diff --git a/extensions/libxt_rateest.c b/extensions/libxt_rateest.c
index 2244558..92b8d50 100644
--- a/extensions/libxt_rateest.c
+++ b/extensions/libxt_rateest.c
@@ -8,9 +8,6 @@
 #include <xtables.h>
 #include <linux/netfilter/xt_rateest.h>
 
-/* Ugly hack to pass info to final_check function. We should fix the API */
-static struct xt_rateest_match_info *rateest_info;
-
 static void rateest_help(void)
 {
 	printf(
@@ -29,8 +26,11 @@ static void rateest_help(void)
 }
 
 enum rateest_options {
+	OPT_RATEEST_A,
 	OPT_RATEEST1,
 	OPT_RATEEST2,
+	OPT_RATEEST_ABPS,
+	OPT_RATEEST_APPS,
 	OPT_RATEEST_BPS1,
 	OPT_RATEEST_PPS1,
 	OPT_RATEEST_BPS2,
@@ -41,26 +41,71 @@ enum rateest_options {
 	OPT_RATEEST_EQ,
 	OPT_RATEEST_BYTES,
 	OPT_RATEEST_PACKETS,
+	F_RATEEST_A       = 1 << OPT_RATEEST_A,
+	F_RATEEST1        = 1 << OPT_RATEEST1,
+	F_RATEEST2        = 1 << OPT_RATEEST2,
+	F_RATEEST_ABPS    = 1 << OPT_RATEEST_ABPS,
+	F_RATEEST_APPS    = 1 << OPT_RATEEST_APPS,
+	F_RATEEST_BPS1    = 1 << OPT_RATEEST_BPS1,
+	F_RATEEST_BPS2    = 1 << OPT_RATEEST_BPS2,
+	F_RATEEST_PPS1    = 1 << OPT_RATEEST_PPS1,
+	F_RATEEST_PPS2    = 1 << OPT_RATEEST_PPS2,
+	F_RATEEST_BYTES   = 1 << OPT_RATEEST_BYTES,
+	F_RATEEST_PACKETS = 1 << OPT_RATEEST_PACKETS,
 };
 
-static const struct option rateest_opts[] = {
-	{.name = "rateest1",      .has_arg = true,  .val = OPT_RATEEST1},
-	{.name = "rateest",       .has_arg = true,  .val = OPT_RATEEST1}, /* alias for absolute mode */
-	{.name = "rateest2",      .has_arg = true,  .val = OPT_RATEEST2},
-	{.name = "rateest-bps1",  .has_arg = true, .val = OPT_RATEEST_BPS1},
-	{.name = "rateest-pps1",  .has_arg = true, .val = OPT_RATEEST_PPS1},
-	{.name = "rateest-bps2",  .has_arg = true, .val = OPT_RATEEST_BPS2},
-	{.name = "rateest-pps2",  .has_arg = true, .val = OPT_RATEEST_PPS2},
-	{.name = "rateest-bps",   .has_arg = true, .val = OPT_RATEEST_BPS2}, /* alias for absolute mode */
-	{.name = "rateest-pps",   .has_arg = true, .val = OPT_RATEEST_PPS2}, /* alias for absolute mode */
-	{.name = "rateest-delta", .has_arg = false, .val = OPT_RATEEST_DELTA},
-	{.name = "rateest-lt",    .has_arg = false, .val = OPT_RATEEST_LT},
-	{.name = "rateest-gt",    .has_arg = false, .val = OPT_RATEEST_GT},
-	{.name = "rateest-eq",    .has_arg = false, .val = OPT_RATEEST_EQ},
-	{.name = "bytes",   .has_arg = false, .val = OPT_RATEEST_BYTES},
-	{.name = "packets", .has_arg = false, .val = OPT_RATEEST_PACKETS},
-	XT_GETOPT_TABLEEND,
+#define s struct xt_rateest_match_info
+static const struct xt_option_entry rateest_opts[] = {
+	{.name = "rateest-delta", .id = OPT_RATEEST_DELTA, .type = XTTYPE_NONE},
+	{.name = "rateest-lt", .id = OPT_RATEEST_LT, .type = XTTYPE_NONE,
+	 .flags = XTOPT_INVERT},
+	{.name = "rateest-gt", .id = OPT_RATEEST_GT, .type = XTTYPE_NONE,
+	 .flags = XTOPT_INVERT},
+	{.name = "rateest-eq", .id = OPT_RATEEST_EQ, .type = XTTYPE_NONE,
+	 .flags = XTOPT_INVERT},
+
+	{.name = "rateest", .id = OPT_RATEEST_A, .type = XTTYPE_STRING,
+	 .excl = F_RATEEST1 | F_RATEEST2,
+	 .flags = XTOPT_PUT, XTOPT_POINTER(s, name1)},
+	{.name = "rateest1", .id = OPT_RATEEST1, .type = XTTYPE_STRING,
+	 .excl = F_RATEEST_A, .also = F_RATEEST2,
+	 .flags = XTOPT_PUT, XTOPT_POINTER(s, name1)},
+	{.name = "rateest2", .id = OPT_RATEEST2, .type = XTTYPE_STRING,
+	 .excl = F_RATEEST_A, .also = F_RATEEST1,
+	 .flags = XTOPT_PUT, XTOPT_POINTER(s, name2)},
+
+	{.name = "rateest-bps", .id = OPT_RATEEST_ABPS, .type = XTTYPE_STRING,
+	 .excl = F_RATEEST_BPS1 | F_RATEEST_BPS2 | F_RATEEST_APPS,
+	 F_RATEEST_PPS1 | F_RATEEST_PPS2 | F_RATEEST_BYTES | F_RATEEST_PACKETS,
+	 .also = F_RATEEST_A},
+	{.name = "rateest-pps", .id = OPT_RATEEST_APPS, .type = XTTYPE_UINT32,
+	 .excl = F_RATEEST_BPS1 | F_RATEEST_BPS2 | F_RATEEST_ABPS,
+	 F_RATEEST_PPS1 | F_RATEEST_PPS2 | F_RATEEST_BYTES | F_RATEEST_PACKETS,
+	 .also = F_RATEEST_A, .flags = XTOPT_PUT, XTOPT_POINTER(s, pps2)},
+	{.name = "bytes", .id = OPT_RATEEST_BYTES, .type = XTTYPE_NONE,
+	 .excl = F_RATEEST_PACKETS, .also = F_RATEEST2},
+	{.name = "packets", .id = OPT_RATEEST_PACKETS, .type = XTTYPE_NONE,
+	 .excl = F_RATEEST_BYTES, .also = F_RATEEST2},
+
+	{.name = "rateest-bps1", .id = OPT_RATEEST_BPS1, .type = XTTYPE_STRING,
+	 .excl = F_RATEEST_ABPS | F_RATEEST_APPS | F_RATEEST_PPS1 |
+	  F_RATEEST_PPS2 | F_RATEEST_BYTES | F_RATEEST_PACKETS,
+	 .also = F_RATEEST_BPS2},
+	{.name = "rateest-bps2", .id = OPT_RATEEST_BPS2, .type = XTTYPE_STRING,
+	 .excl = F_RATEEST_ABPS | F_RATEEST_APPS | F_RATEEST_PPS1 |
+	 F_RATEEST_PPS2 | F_RATEEST_BYTES | F_RATEEST_PACKETS,
+	 .also = F_RATEEST_BPS1},
+	{.name = "rateest-pps1", .id = OPT_RATEEST_PPS1, .type = XTTYPE_UINT32,
+	 .excl = F_RATEEST_ABPS | F_RATEEST_APPS | F_RATEEST_BPS1 |
+	 F_RATEEST_BPS2 | F_RATEEST_BYTES | F_RATEEST_PACKETS,
+	 .also = F_RATEEST_PPS2, .flags = XTOPT_PUT, XTOPT_POINTER(s, pps1)},
+	{.name = "rateest-pps2", .id = OPT_RATEEST_PPS2, .type = XTTYPE_UINT32,
+	 .excl = F_RATEEST_ABPS | F_RATEEST_APPS | F_RATEEST_BPS1 |
+	 F_RATEEST_BPS2 | F_RATEEST_BYTES | F_RATEEST_PACKETS,
+	 .also = F_RATEEST_PPS1, .flags = XTOPT_PUT, XTOPT_POINTER(s, pps2)},
+	XTOPT_TABLEEND,
 };
+#undef s
 
 /* Copied from iproute. See http://physics.nist.gov/cuu/Units/binary.html */
 static const struct rate_suffix {
@@ -113,173 +158,51 @@ rateest_get_rate(uint32_t *rate, const char *str)
 	return -1;
 }
 
-static int
-rateest_parse(int c, char **argv, int invert, unsigned int *flags,
-	      const void *entry, struct xt_entry_match **match)
+static void rateest_parse(struct xt_option_call *cb)
 {
-	struct xt_rateest_match_info *info = (void *)(*match)->data;
-	unsigned int val;
-
-	rateest_info = info;
+	struct xt_rateest_match_info *info = cb->data;
 
-	switch (c) {
+	xtables_option_parse(cb);
+	switch (cb->entry->id) {
 	case OPT_RATEEST1:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest1 twice");
-		*flags |= 1 << c;
-
-		strncpy(info->name1, optarg, sizeof(info->name1) - 1);
-		break;
-
 	case OPT_RATEEST2:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest2 twice");
-		*flags |= 1 << c;
-
-		strncpy(info->name2, optarg, sizeof(info->name2) - 1);
 		info->flags |= XT_RATEEST_MATCH_REL;
 		break;
-
+	case OPT_RATEEST_APPS:
+	case OPT_RATEEST_PPS1:
+	case OPT_RATEEST_PPS2:
+		info->flags |= XT_RATEEST_MATCH_PPS;
+		break;
+	case OPT_RATEEST_ABPS:
 	case OPT_RATEEST_BPS1:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest-bps can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest-bps1 twice");
-		*flags |= 1 << c;
-
 		info->flags |= XT_RATEEST_MATCH_BPS;
-
-		if (rateest_get_rate(&info->bps1, optarg) < 0)
+		if (rateest_get_rate(&info->bps1, cb->arg) < 0)
 			xtables_error(PARAMETER_PROBLEM,
 				   "rateest: could not parse rate `%s'",
-				   optarg);
-		break;
-
-	case OPT_RATEEST_PPS1:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest-pps can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest-pps1 twice");
-		*flags |= 1 << c;
-
-		info->flags |= XT_RATEEST_MATCH_PPS;
-
-		if (!xtables_strtoui(optarg, NULL, &val, 0, UINT32_MAX))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: could not parse pps `%s'",
-				   optarg);
-		info->pps1 = val;
-		break;
-
+				   cb->arg);
 	case OPT_RATEEST_BPS2:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest-bps can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest-bps2 twice");
-		*flags |= 1 << c;
-
 		info->flags |= XT_RATEEST_MATCH_BPS;
-
-		if (rateest_get_rate(&info->bps2, optarg) < 0)
+		if (rateest_get_rate(&info->bps2, cb->arg) < 0)
 			xtables_error(PARAMETER_PROBLEM,
 				   "rateest: could not parse rate `%s'",
-				   optarg);
+				   cb->arg);
 		break;
-
-	case OPT_RATEEST_PPS2:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest-pps can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest-pps2 twice");
-		*flags |= 1 << c;
-
-		info->flags |= XT_RATEEST_MATCH_PPS;
-
-		if (!xtables_strtoui(optarg, NULL, &val, 0, UINT32_MAX))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: could not parse pps `%s'",
-				   optarg);
-		info->pps2 = val;
-		break;
-
 	case OPT_RATEEST_DELTA:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: rateest-delta can't be inverted");
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify --rateest-delta twice");
-		*flags |= 1 << c;
-
 		info->flags |= XT_RATEEST_MATCH_DELTA;
 		break;
-
 	case OPT_RATEEST_EQ:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify lt/gt/eq twice");
-		*flags |= 1 << c;
-
 		info->mode = XT_RATEEST_MATCH_EQ;
-		if (invert)
+		if (cb->invert)
 			info->flags |= XT_RATEEST_MATCH_INVERT;
 		break;
-
 	case OPT_RATEEST_LT:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify lt/gt/eq twice");
-		*flags |= 1 << c;
-
 		info->mode = XT_RATEEST_MATCH_LT;
-		if (invert)
+		if (cb->invert)
 			info->flags |= XT_RATEEST_MATCH_INVERT;
 		break;
-
 	case OPT_RATEEST_GT:
-		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
-
-		if (*flags & (1 << c))
-			xtables_error(PARAMETER_PROBLEM,
-				   "rateest: can't specify lt/gt/eq twice");
-		*flags |= 1 << c;
-
 		info->mode = XT_RATEEST_MATCH_GT;
-		if (invert)
+		if (cb->invert)
 			info->flags |= XT_RATEEST_MATCH_INVERT;
 		break;
 
@@ -291,16 +214,13 @@ rateest_parse(int c, char **argv, int invert, unsigned int *flags,
 		info->flags |= XT_RATEEST_MATCH_PPS;
 		break;
 	}
-
-	return 1;
 }
 
-static void
-rateest_final_check(unsigned int flags)
+static void rateest_final_check(struct xt_fcheck_call *cb)
 {
-	struct xt_rateest_match_info *info = rateest_info;
+	struct xt_rateest_match_info *info = cb->data;
 
-	if (info == NULL)
+	if (cb->xflags == 0)
 		xtables_error(PARAMETER_PROBLEM, "rateest match: "
 		           "you need to specify some flags");
 	if (!(info->flags & XT_RATEEST_MATCH_REL))
@@ -468,11 +388,11 @@ static struct xtables_match rateest_mt_reg = {
 	.size		= XT_ALIGN(sizeof(struct xt_rateest_match_info)),
 	.userspacesize	= XT_ALIGN(offsetof(struct xt_rateest_match_info, est1)),
 	.help		= rateest_help,
-	.parse		= rateest_parse,
-	.final_check	= rateest_final_check,
+	.x6_parse	= rateest_parse,
+	.x6_fcheck	= rateest_final_check,
 	.print		= rateest_print,
 	.save		= rateest_save,
-	.extra_opts	= rateest_opts,
+	.x6_options	= rateest_opts,
 };
 
 void _init(void)
-- 
1.7.3.4


  parent reply	other threads:[~2011-05-23 14:39 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-23 14:39 option parsing, run 9 Jan Engelhardt
2011-05-23 14:39 ` [PATCH 01/13] libxtables: retract _NE types and use a flag instead Jan Engelhardt
2011-05-23 14:39 ` [PATCH 02/13] libipt_REDIRECT: "--to-ports" is not mandatory Jan Engelhardt
2011-05-23 14:39 ` [PATCH 03/13] libxt_quota: readd missing XTOPT_PUT request Jan Engelhardt
2011-05-23 14:39 ` [PATCH 04/13] libxt_quota: make sure uint64 is not truncated Jan Engelhardt
2011-05-23 14:39 ` [PATCH 05/13] libxtables: check for negative numbers in xtables_strtou* Jan Engelhardt
2011-05-23 14:39 ` [PATCH 06/13] libxt_rateest: streamline case display of units Jan Engelhardt
2011-05-23 14:39 ` [PATCH 07/13] doc: add some coded option examples to libxt_hashlimit Jan Engelhardt
2011-05-23 14:39 ` [PATCH 08/13] doc: make usage of libxt_rateest more obvious Jan Engelhardt
2011-05-23 14:39 ` [PATCH 09/13] doc: clarify that -p all is a special keyword only Jan Engelhardt
2011-05-23 14:39 ` [PATCH 10/13] libxt_rateest: avoid optional arguments Jan Engelhardt
2011-05-24  6:46   ` Patrick McHardy
2011-05-24  8:03     ` Jan Engelhardt
2011-05-24  8:14       ` Patrick McHardy
2011-05-24  8:51         ` Jan Engelhardt
2011-05-24 12:49           ` Patrick McHardy
2011-05-23 14:39 ` [PATCH 11/13] libxt_rateest: fix rateest save operation Jan Engelhardt
2011-05-23 14:39 ` Jan Engelhardt [this message]
2011-05-23 14:39 ` [PATCH 13/13] libxt_ipvs: restore network-byte order Jan Engelhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1306161564-4370-13-git-send-email-jengelh@medozas.de \
    --to=jengelh@medozas.de \
    --cc=kaber@trash.net \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).