[iptables,3/3] xtables: Introduce and use common function to print val[/mask] arguments

Message ID 1519902192-17312-4-git-send-email-serhe.popovych@gmail.com
State Accepted
Delegated to: Pablo Neira
Headers show
Series
  • iptables: Unify val[/mask] parsing and printing routines
Related show

Commit Message

Serhey Popovych March 1, 2018, 11:03 a.m.
There are number of places where argument is in val[/mask] format
printed in extensions and some of them may print corresponding symbolic
name.

By introducing common function for this task we eliminate custom code
parts in extensions to perform printing of arguments in required
formats.

Use xtables_print_mark_mask() helper for extensions without
symbolic name for val[/mask].

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
---
 extensions/libipt_realm.c   |   21 ++-------------------
 extensions/libxt_connmark.c |   18 ++++++------------
 extensions/libxt_devgroup.c |   27 ++++++---------------------
 extensions/libxt_mark.c     |   17 +++++------------
 include/xtables.h           |    9 +++++++++
 libxtables/xtables.c        |   20 ++++++++++++++++++++
 6 files changed, 48 insertions(+), 64 deletions(-)

Comments

Florian Westphal April 27, 2018, 9:08 p.m. | #1
Serhey Popovych <serhe.popovych@gmail.com> wrote:
> There are number of places where argument is in val[/mask] format
> printed in extensions and some of them may print corresponding symbolic
> name.
> 
Also applied, thank you.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/extensions/libipt_realm.c b/extensions/libipt_realm.c
index 545b258..e01d048 100644
--- a/extensions/libipt_realm.c
+++ b/extensions/libipt_realm.c
@@ -47,23 +47,6 @@  static void realm_parse(struct xt_option_call *cb)
 		ri->invert = 1;
 }
 
-static void
-print_realm(unsigned long id, unsigned long mask, int numeric)
-{
-	const char *name = NULL;
-
-	if (mask != 0xffffffff)
-		printf(" 0x%lx/0x%lx", id, mask);
-	else {
-		if (numeric == 0)
-			name = xtables_lmap_id2name(realms, id);
-		if (name)
-			printf(" %s", name);
-		else
-			printf(" 0x%lx", id);
-	}
-}
-
 static void realm_print(const void *ip, const struct xt_entry_match *match,
 			int numeric)
 {
@@ -73,7 +56,7 @@  static void realm_print(const void *ip, const struct xt_entry_match *match,
 		printf(" !");
 
 	printf(" realm");
-	print_realm(ri->id, ri->mask, numeric);
+	xtables_print_val_mask(ri->id, ri->mask, numeric ? NULL : realms);
 }
 
 static void realm_save(const void *ip, const struct xt_entry_match *match)
@@ -84,7 +67,7 @@  static void realm_save(const void *ip, const struct xt_entry_match *match)
 		printf(" !");
 
 	printf(" --realm");
-	print_realm(ri->id, ri->mask, 0);
+	xtables_print_val_mask(ri->id, ri->mask, realms);
 }
 
 static void
diff --git a/extensions/libxt_connmark.c b/extensions/libxt_connmark.c
index be3499b..cb4264e 100644
--- a/extensions/libxt_connmark.c
+++ b/extensions/libxt_connmark.c
@@ -69,14 +69,6 @@  static void connmark_parse(struct xt_option_call *cb)
 		markinfo->invert = 1;
 }
 
-static void print_mark(unsigned int mark, unsigned int mask)
-{
-	if (mask != 0xffffffffU)
-		printf(" 0x%x/0x%x", mark, mask);
-	else
-		printf(" 0x%x", mark);
-}
-
 static void
 connmark_print(const void *ip, const struct xt_entry_match *match, int numeric)
 {
@@ -85,7 +77,8 @@  connmark_print(const void *ip, const struct xt_entry_match *match, int numeric)
 	printf(" CONNMARK match ");
 	if (info->invert)
 		printf("!");
-	print_mark(info->mark, info->mask);
+
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void
@@ -97,7 +90,8 @@  connmark_mt_print(const void *ip, const struct xt_entry_match *match,
 	printf(" connmark match ");
 	if (info->invert)
 		printf("!");
-	print_mark(info->mark, info->mask);
+
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void connmark_save(const void *ip, const struct xt_entry_match *match)
@@ -108,7 +102,7 @@  static void connmark_save(const void *ip, const struct xt_entry_match *match)
 		printf(" !");
 
 	printf(" --mark");
-	print_mark(info->mark, info->mask);
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void
@@ -120,7 +114,7 @@  connmark_mt_save(const void *ip, const struct xt_entry_match *match)
 		printf(" !");
 
 	printf(" --mark");
-	print_mark(info->mark, info->mask);
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void print_mark_xlate(unsigned int mark, unsigned int mask,
diff --git a/extensions/libxt_devgroup.c b/extensions/libxt_devgroup.c
index 99a4d50..a88211c 100644
--- a/extensions/libxt_devgroup.c
+++ b/extensions/libxt_devgroup.c
@@ -61,38 +61,23 @@  static void devgroup_parse(struct xt_option_call *cb)
 	}
 }
 
-static void
-print_devgroup(unsigned int id, unsigned int mask, int numeric)
-{
-	const char *name = NULL;
-
-	if (mask != 0xffffffff)
-		printf("0x%x/0x%x", id, mask);
-	else {
-		if (numeric == 0)
-			name = xtables_lmap_id2name(devgroups, id);
-		if (name)
-			printf("%s", name);
-		else
-			printf("0x%x", id);
-	}
-}
-
 static void devgroup_show(const char *pfx, const struct xt_devgroup_info *info,
 			  int numeric)
 {
 	if (info->flags & XT_DEVGROUP_MATCH_SRC) {
 		if (info->flags & XT_DEVGROUP_INVERT_SRC)
 			printf(" !");
-		printf(" %ssrc-group ", pfx);
-		print_devgroup(info->src_group, info->src_mask, numeric);
+		printf(" %ssrc-group", pfx);
+		xtables_print_val_mask(info->src_group, info->src_mask,
+				       numeric ? NULL : devgroups);
 	}
 
 	if (info->flags & XT_DEVGROUP_MATCH_DST) {
 		if (info->flags & XT_DEVGROUP_INVERT_DST)
 			printf(" !");
-		printf(" %sdst-group ", pfx);
-		print_devgroup(info->dst_group, info->dst_mask, numeric);
+		printf(" %sdst-group", pfx);
+		xtables_print_val_mask(info->dst_group, info->dst_mask,
+				       numeric ? NULL : devgroups);
 	}
 }
 
diff --git a/extensions/libxt_mark.c b/extensions/libxt_mark.c
index e1d00de..134ad43 100644
--- a/extensions/libxt_mark.c
+++ b/extensions/libxt_mark.c
@@ -47,14 +47,6 @@  static void mark_parse(struct xt_option_call *cb)
 	markinfo->mask = cb->val.mask;
 }
 
-static void print_mark(unsigned int mark, unsigned int mask)
-{
-	if (mask != 0xffffffffU)
-		printf(" 0x%x/0x%x", mark, mask);
-	else
-		printf(" 0x%x", mark);
-}
-
 static void
 mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
 {
@@ -63,7 +55,8 @@  mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
 	printf(" mark match");
 	if (info->invert)
 		printf(" !");
-	print_mark(info->mark, info->mask);
+
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void
@@ -76,7 +69,7 @@  mark_print(const void *ip, const struct xt_entry_match *match, int numeric)
 	if (info->invert)
 		printf(" !");
 
-	print_mark(info->mark, info->mask);
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void mark_mt_save(const void *ip, const struct xt_entry_match *match)
@@ -87,7 +80,7 @@  static void mark_mt_save(const void *ip, const struct xt_entry_match *match)
 		printf(" !");
 
 	printf(" --mark");
-	print_mark(info->mark, info->mask);
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void
@@ -99,7 +92,7 @@  mark_save(const void *ip, const struct xt_entry_match *match)
 		printf(" !");
 
 	printf(" --mark");
-	print_mark(info->mark, info->mask);
+	xtables_print_mark_mask(info->mark, info->mask);
 }
 
 static void
diff --git a/include/xtables.h b/include/xtables.h
index 4712948..304828a 100644
--- a/include/xtables.h
+++ b/include/xtables.h
@@ -548,6 +548,15 @@  static inline void xtables_parse_mark_mask(struct xt_option_call *cb,
 	xtables_parse_val_mask(cb, mark, mask, NULL);
 }
 
+extern void xtables_print_val_mask(unsigned int val, unsigned int mask,
+				   const struct xtables_lmap *lmap);
+
+static inline void xtables_print_mark_mask(unsigned int mark,
+					   unsigned int mask)
+{
+	xtables_print_val_mask(mark, mask, NULL);
+}
+
 #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
 #	ifdef _INIT
 #		undef _init
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index f93e88d..60a0231 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -2012,6 +2012,26 @@  name2val:
 			cb->ext_name, cb->arg, cb->entry->name);
 }
 
+void xtables_print_val_mask(unsigned int val, unsigned int mask,
+			    const struct xtables_lmap *lmap)
+{
+	if (mask != ~0U) {
+		printf(" 0x%x/0x%x", val, mask);
+		return;
+	}
+
+	if (lmap) {
+		const char *name = xtables_lmap_id2name(lmap, val);
+
+		if (name) {
+			printf(" %s", name);
+			return;
+		}
+	}
+
+	printf(" 0x%x", val);
+}
+
 int kernel_version;
 
 void get_kernel_version(void)