From: Jan Engelhardt <jengelh@medozas.de>
To: kaber@trash.net
Cc: netfilter-devel@vger.kernel.org
Subject: [PATCH 06/28] libip6t_frag: use guided option parser
Date: Tue, 12 Apr 2011 16:23:21 +0200 [thread overview]
Message-ID: <1302618224-9449-7-git-send-email-jengelh@medozas.de> (raw)
In-Reply-To: <1302618224-9449-1-git-send-email-jengelh@medozas.de>
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
---
extensions/libip6t_frag.c | 145 +++++++++++----------------------------------
1 files changed, 36 insertions(+), 109 deletions(-)
diff --git a/extensions/libip6t_frag.c b/extensions/libip6t_frag.c
index 19aca4c..12794e4 100644
--- a/extensions/libip6t_frag.c
+++ b/extensions/libip6t_frag.c
@@ -1,14 +1,18 @@
-/* Shared library add-on to ip6tables to add Fragmentation header support. */
-#include <stdbool.h>
#include <stdio.h>
-#include <netdb.h>
-#include <string.h>
-#include <stdlib.h>
-#include <getopt.h>
-#include <errno.h>
#include <xtables.h>
#include <linux/netfilter_ipv6/ip6t_frag.h>
+enum {
+ O_FRAGID = 0,
+ O_FRAGLEN,
+ O_FRAGRES,
+ O_FRAGFIRST,
+ O_FRAGMORE,
+ O_FRAGLAST,
+ F_FRAGMORE = 1 << O_FRAGMORE,
+ F_FRAGLAST = 1 << O_FRAGLAST,
+};
+
static void frag_help(void)
{
printf(
@@ -21,58 +25,21 @@ static void frag_help(void)
" is the last one\n");
}
-static const struct option frag_opts[] = {
- {.name = "fragid", .has_arg = true, .val = '1'},
- {.name = "fraglen", .has_arg = true, .val = '2'},
- {.name = "fragres", .has_arg = false, .val = '3'},
- {.name = "fragfirst", .has_arg = false, .val = '4'},
- {.name = "fragmore", .has_arg = false, .val = '5'},
- {.name = "fraglast", .has_arg = false, .val = '6'},
- XT_GETOPT_TABLEEND,
+#define s struct ip6t_frag
+static const struct xt_option_entry frag_opts[] = {
+ {.name = "fragid", .id = O_FRAGID, .type = XTTYPE_UINT32RC,
+ .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, ids)},
+ {.name = "fraglen", .id = O_FRAGLEN, .type = XTTYPE_UINT32,
+ .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, hdrlen)},
+ {.name = "fragres", .id = O_FRAGRES, .type = XTTYPE_NONE},
+ {.name = "fragfirst", .id = O_FRAGFIRST, .type = XTTYPE_NONE},
+ {.name = "fragmore", .id = O_FRAGMORE, .type = XTTYPE_NONE,
+ .excl = F_FRAGLAST},
+ {.name = "fraglast", .id = O_FRAGLAST, .type = XTTYPE_NONE,
+ .excl = F_FRAGMORE},
+ XTOPT_TABLEEND,
};
-
-static uint32_t
-parse_frag_id(const char *idstr, const char *typestr)
-{
- unsigned long int id;
- char* ep;
-
- id = strtoul(idstr, &ep, 0);
-
- if ( idstr == ep ) {
- xtables_error(PARAMETER_PROBLEM,
- "FRAG no valid digits in %s `%s'", typestr, idstr);
- }
- if ( id == ULONG_MAX && errno == ERANGE ) {
- xtables_error(PARAMETER_PROBLEM,
- "%s `%s' specified too big: would overflow",
- typestr, idstr);
- }
- if ( *idstr != '\0' && *ep != '\0' ) {
- xtables_error(PARAMETER_PROBLEM,
- "FRAG error parsing %s `%s'", typestr, idstr);
- }
- return id;
-}
-
-static void
-parse_frag_ids(const char *idstring, uint32_t *ids)
-{
- char *buffer;
- char *cp;
-
- buffer = strdup(idstring);
- if ((cp = strchr(buffer, ':')) == NULL)
- ids[0] = ids[1] = parse_frag_id(buffer,"id");
- else {
- *cp = '\0';
- cp++;
-
- ids[0] = buffer[0] ? parse_frag_id(buffer,"id") : 0;
- ids[1] = cp[0] ? parse_frag_id(cp,"id") : 0xFFFFFFFF;
- }
- free(buffer);
-}
+#undef s
static void frag_init(struct xt_entry_match *m)
{
@@ -81,65 +48,25 @@ static void frag_init(struct xt_entry_match *m)
fraginfo->ids[1] = 0xFFFFFFFF;
}
-static int frag_parse(int c, char **argv, int invert, unsigned int *flags,
- const void *entry, struct xt_entry_match **match)
+static void frag_parse(struct xt_option_call *cb)
{
- struct ip6t_frag *fraginfo = (struct ip6t_frag *)(*match)->data;
-
- switch (c) {
- case '1':
- if (*flags & IP6T_FRAG_IDS)
- xtables_error(PARAMETER_PROBLEM,
- "Only one `--fragid' allowed");
- xtables_check_inverse(optarg, &invert, &optind, 0, argv);
- parse_frag_ids(optarg, fraginfo->ids);
- if (invert)
- fraginfo->invflags |= IP6T_FRAG_INV_IDS;
- fraginfo->flags |= IP6T_FRAG_IDS;
- *flags |= IP6T_FRAG_IDS;
- break;
- case '2':
- if (*flags & IP6T_FRAG_LEN)
- xtables_error(PARAMETER_PROBLEM,
- "Only one `--fraglen' allowed");
- xtables_check_inverse(optarg, &invert, &optind, 0, argv);
- fraginfo->hdrlen = parse_frag_id(optarg, "length");
- if (invert)
- fraginfo->invflags |= IP6T_FRAG_INV_LEN;
- fraginfo->flags |= IP6T_FRAG_LEN;
- *flags |= IP6T_FRAG_LEN;
- break;
- case '3':
- if (*flags & IP6T_FRAG_RES)
- xtables_error(PARAMETER_PROBLEM,
- "Only one `--fragres' allowed");
+ struct ip6t_frag *fraginfo = cb->data;
+
+ xtables_option_parse(cb);
+ switch (cb->entry->id) {
+ case O_FRAGRES:
fraginfo->flags |= IP6T_FRAG_RES;
- *flags |= IP6T_FRAG_RES;
break;
- case '4':
- if (*flags & IP6T_FRAG_FST)
- xtables_error(PARAMETER_PROBLEM,
- "Only one `--fragfirst' allowed");
+ case O_FRAGFIRST:
fraginfo->flags |= IP6T_FRAG_FST;
- *flags |= IP6T_FRAG_FST;
break;
- case '5':
- if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF))
- xtables_error(PARAMETER_PROBLEM,
- "Only one `--fragmore' or `--fraglast' allowed");
+ case O_FRAGMORE:
fraginfo->flags |= IP6T_FRAG_MF;
- *flags |= IP6T_FRAG_MF;
break;
- case '6':
- if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF))
- xtables_error(PARAMETER_PROBLEM,
- "Only one `--fragmore' or `--fraglast' allowed");
+ case O_FRAGLAST:
fraginfo->flags |= IP6T_FRAG_NMF;
- *flags |= IP6T_FRAG_NMF;
break;
}
-
- return 1;
}
static void
@@ -234,10 +161,10 @@ static struct xtables_match frag_mt6_reg = {
.userspacesize = XT_ALIGN(sizeof(struct ip6t_frag)),
.help = frag_help,
.init = frag_init,
- .parse = frag_parse,
.print = frag_print,
.save = frag_save,
- .extra_opts = frag_opts,
+ .x6_parse = frag_parse,
+ .x6_options = frag_opts,
};
void
--
1.7.1
next prev parent reply other threads:[~2011-04-12 14:23 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-12 14:23 guided option parser, run 2 Jan Engelhardt
2011-04-12 14:23 ` [PATCH 01/28] libxtables: XTTYPE_UINT8 support Jan Engelhardt
2011-04-12 14:23 ` [PATCH 02/28] libip[6]t_HL: use guided option parser Jan Engelhardt
2011-04-12 14:23 ` [PATCH 03/28] libip[6]t_hl: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 04/28] libxtables: XTTYPE_UINT32RC support Jan Engelhardt
2011-04-12 14:23 ` [PATCH 05/28] libip[6]t_ah: use guided option parser Jan Engelhardt
2011-04-12 14:23 ` Jan Engelhardt [this message]
2011-04-12 14:23 ` [PATCH 07/28] libxt_esp: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 08/28] libxtables: XTTYPE_STRING support Jan Engelhardt
2011-04-12 14:23 ` [PATCH 09/28] libip[6]t_REJECT: use guided option parser Jan Engelhardt
2011-04-12 14:23 ` [PATCH 10/28] libip6t_dst: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 11/28] libip6t_hbh: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 12/28] libip[6]t_icmp: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 13/28] libip6t_ipv6header: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 14/28] libipt_ECN: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 15/28] libipt_addrtype: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 16/28] libxt_AUDIT: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 17/28] libxt_CLASSIFY: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 18/28] libxt_DSCP: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 19/28] libxt_LED: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 20/28] libxt_SECMARK: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 21/28] libxt_TCPOPTSTRIP: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 22/28] libxt_comment: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 23/28] libxt_helper: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 24/28] libxt_physdev: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 25/28] libxt_pkttype: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 26/28] libxt_state: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 27/28] libxt_time: " Jan Engelhardt
2011-04-12 14:23 ` [PATCH 28/28] libxt_u32: " Jan Engelhardt
2011-04-13 11:41 ` guided option parser, run 2 Patrick McHardy
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=1302618224-9449-7-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).