Linux Netfilter development
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: netfilter-devel@vger.kernel.org
Subject: [iptables PATCH 12/23] extensions: libebt_log: Use guided option parser
Date: Wed, 20 Dec 2023 17:06:25 +0100	[thread overview]
Message-ID: <20231220160636.11778-13-phil@nwl.cc> (raw)
In-Reply-To: <20231220160636.11778-1-phil@nwl.cc>

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 extensions/libebt_log.c | 121 ++++++++++------------------------------
 1 file changed, 30 insertions(+), 91 deletions(-)

diff --git a/extensions/libebt_log.c b/extensions/libebt_log.c
index 9f8d158956802..dc2357c022bf0 100644
--- a/extensions/libebt_log.c
+++ b/extensions/libebt_log.c
@@ -14,19 +14,11 @@
 #include <stdlib.h>
 #include <syslog.h>
 #include <string.h>
-#include <getopt.h>
 #include <xtables.h>
 #include <linux/netfilter_bridge/ebt_log.h>
 
 #define LOG_DEFAULT_LEVEL LOG_INFO
 
-#define LOG_PREFIX '1'
-#define LOG_LEVEL  '2'
-#define LOG_ARP    '3'
-#define LOG_IP     '4'
-#define LOG_LOG    '5'
-#define LOG_IP6    '6'
-
 struct code {
 	char *c_name;
 	int c_val;
@@ -43,26 +35,26 @@ static struct code eight_priority[] = {
 	{ "debug", LOG_DEBUG }
 };
 
-static int name_to_loglevel(const char *arg)
-{
-	int i;
-
-	for (i = 0; i < 8; i++)
-		if (!strcmp(arg, eight_priority[i].c_name))
-			return eight_priority[i].c_val;
-
-	/* return bad loglevel */
-	return 9;
-}
+enum {
+	/* first three must correspond with bit pos in respective EBT_LOG_* */
+	O_LOG_IP = 0,
+	O_LOG_ARP = 1,
+	O_LOG_IP6 = 3,
+	O_LOG_PREFIX,
+	O_LOG_LEVEL,
+	O_LOG_LOG,
+};
 
-static const struct option brlog_opts[] = {
-	{ .name = "log-prefix",		.has_arg = true,  .val = LOG_PREFIX },
-	{ .name = "log-level",		.has_arg = true,  .val = LOG_LEVEL  },
-	{ .name = "log-arp",		.has_arg = false, .val = LOG_ARP    },
-	{ .name = "log-ip",		.has_arg = false, .val = LOG_IP     },
-	{ .name = "log",		.has_arg = false, .val = LOG_LOG    },
-	{ .name = "log-ip6",		.has_arg = false, .val = LOG_IP6    },
-	XT_GETOPT_TABLEEND,
+static const struct xt_option_entry brlog_opts[] = {
+	{ .name = "log-prefix", .id = O_LOG_PREFIX, .type = XTTYPE_STRING,
+	  .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_log_info, prefix) },
+	{ .name = "log-level", .id = O_LOG_LEVEL, .type = XTTYPE_SYSLOGLEVEL,
+	  .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_log_info, loglevel) },
+	{ .name = "log-arp",	.id = O_LOG_ARP,	.type = XTTYPE_NONE },
+	{ .name = "log-ip",	.id = O_LOG_IP,		.type = XTTYPE_NONE },
+	{ .name = "log",	.id = O_LOG_LOG,	.type = XTTYPE_NONE },
+	{ .name = "log-ip6",	.id = O_LOG_IP6,	.type = XTTYPE_NONE },
+	XTOPT_TABLEEND,
 };
 
 static void brlog_help(void)
@@ -87,73 +79,21 @@ static void brlog_init(struct xt_entry_target *t)
 {
 	struct ebt_log_info *loginfo = (struct ebt_log_info *)t->data;
 
-	loginfo->bitmask = 0;
-	loginfo->prefix[0] = '\0';
 	loginfo->loglevel = LOG_NOTICE;
 }
 
-static unsigned int log_chk_inv(int inv, unsigned int bit, const char *suffix)
-{
-	if (inv)
-		xtables_error(PARAMETER_PROBLEM,
-			      "Unexpected `!' after --log%s", suffix);
-	return bit;
-}
-
-static int brlog_parse(int c, char **argv, int invert, unsigned int *flags,
-		       const void *entry, struct xt_entry_target **target)
+static void brlog_parse(struct xt_option_call *cb)
 {
-	struct ebt_log_info *loginfo = (struct ebt_log_info *)(*target)->data;
-	long int i;
-	char *end;
-
-	switch (c) {
-	case LOG_PREFIX:
-		if (invert)
-			xtables_error(PARAMETER_PROBLEM,
-				      "Unexpected `!` after --log-prefix");
-		if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
-			xtables_error(PARAMETER_PROBLEM,
-				      "Prefix too long");
-		if (strchr(optarg, '\"'))
-			xtables_error(PARAMETER_PROBLEM,
-				      "Use of \\\" is not allowed"
-				      " in the prefix");
-		strcpy((char *)loginfo->prefix, (char *)optarg);
+	struct ebt_log_info *loginfo = cb->data;
+
+	xtables_option_parse(cb);
+	switch (cb->entry->id) {
+	case O_LOG_IP:
+	case O_LOG_ARP:
+	case O_LOG_IP6:
+		loginfo->bitmask |= 1 << cb->entry->id;
 		break;
-	case LOG_LEVEL:
-		i = strtol(optarg, &end, 16);
-		if (*end != '\0' || i < 0 || i > 7)
-			loginfo->loglevel = name_to_loglevel(optarg);
-		else
-			loginfo->loglevel = i;
-
-		if (loginfo->loglevel == 9)
-			xtables_error(PARAMETER_PROBLEM,
-				      "Problem with the log-level");
-		break;
-	case LOG_IP:
-		loginfo->bitmask |= log_chk_inv(invert, EBT_LOG_IP, "-ip");
-		break;
-	case LOG_ARP:
-		loginfo->bitmask |= log_chk_inv(invert, EBT_LOG_ARP, "-arp");
-		break;
-	case LOG_LOG:
-		loginfo->bitmask |= log_chk_inv(invert, 0, "");
-		break;
-	case LOG_IP6:
-		loginfo->bitmask |= log_chk_inv(invert, EBT_LOG_IP6, "-ip6");
-		break;
-	default:
-		return 0;
 	}
-
-	*flags |= loginfo->bitmask;
-	return 1;
-}
-
-static void brlog_final_check(unsigned int flags)
-{
 }
 
 static void brlog_print(const void *ip, const struct xt_entry_target *target,
@@ -204,11 +144,10 @@ static struct xtables_target brlog_target = {
 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_log_info)),
 	.init		= brlog_init,
 	.help		= brlog_help,
-	.parse		= brlog_parse,
-	.final_check	= brlog_final_check,
+	.x6_parse	= brlog_parse,
 	.print		= brlog_print,
 	.xlate		= brlog_xlate,
-	.extra_opts	= brlog_opts,
+	.x6_options	= brlog_opts,
 };
 
 void _init(void)
-- 
2.43.0


  parent reply	other threads:[~2023-12-20 16:06 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-20 16:06 [iptables PATCH 00/23] Guided option parser for ebtables Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 01/23] libxtables: xtoptions: Prevent XTOPT_PUT with XTTYPE_HOSTMASK Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 02/23] libxtables: xtoptions: Support XTOPT_NBO with XTTYPE_UINT* Phil Sutter
2023-12-20 19:07   ` Jan Engelhardt
2023-12-20 21:28     ` Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 03/23] libxtables: xtoptions: Implement XTTYPE_ETHERMACMASK Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 04/23] libxtables: xtoptions: Treat NFPROTO_BRIDGE as IPv4 Phil Sutter
2023-12-20 19:20   ` Jan Engelhardt
2023-12-20 21:35     ` Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 05/23] ebtables: Support for guided option parser Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 06/23] extensions: libebt_*: Drop some needless init callbacks Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 07/23] extensions: libebt_stp: Use guided option parser Phil Sutter
2023-12-20 19:29   ` Jan Engelhardt
2023-12-20 21:47     ` Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 08/23] extensions: libebt_arpreply: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 09/23] extensions: libebt_dnat: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 10/23] extensions: libebt_ip6: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 11/23] extensions: libebt_ip: " Phil Sutter
2023-12-20 16:06 ` Phil Sutter [this message]
2023-12-20 16:06 ` [iptables PATCH 13/23] extensions: libebt_mark: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 14/23] extensions: libebt_nflog: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 15/23] extensions: libebt_snat: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 16/23] extensions: libebt_redirect: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 17/23] extensions: libebt_802_3: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 18/23] extensions: libebt_vlan: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 19/23] extensions: libebt_arp: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 20/23] extensions: libxt_limit: Use guided option parser for NFPROTO_BRIDGE, too Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 21/23] extensions: libebt_pkttype: Use guided option parser Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 22/23] extensions: libebt_mark_m: " Phil Sutter
2023-12-20 16:06 ` [iptables PATCH 23/23] extensions: libxt_HMARK: Review HMARK_parse() Phil Sutter
2024-01-10 15:09 ` [iptables PATCH 00/23] Guided option parser for ebtables Phil Sutter

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=20231220160636.11778-13-phil@nwl.cc \
    --to=phil@nwl.cc \
    --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